llvm-native-core-ext 0.1.0

Extended modules for llvm-native-core: analysis passes, transforms, codegen extras, bitcode, linker, JIT, utilities. Part of the llvm-native workspace (https://crates.io/crates/llvm-native).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! LLVM Loop Load Elimination — eliminates redundant loads in loops
//! by forwarding stored values from previous iterations.
//! Clean-room behavioural reconstruction. Zero LLVM source code consultation.

use llvm_native_core::analysis::LoopInfo;
use llvm_native_core::opcode::Opcode;
use llvm_native_core::value::{SubclassKind, ValueRef};
use std::collections::HashSet;

pub struct LoopLoadElimination {
    num_eliminated: usize,
    num_pairs_examined: usize,
    strict_aliasing: bool,
}

impl LoopLoadElimination {
    pub fn new() -> Self {
        Self {
            num_eliminated: 0,
            num_pairs_examined: 0,
            strict_aliasing: true,
        }
    }

    pub fn run_on_function(&mut self, func: &ValueRef) -> usize {
        self.num_eliminated = 0;
        self.num_pairs_examined = 0;
        let loops = find_function_loops(func);
        for loop_info in &loops {
            let pairs = self.find_eliminatable_loads(func, loop_info);
            for (store, load) in pairs {
                self.num_pairs_examined += 1;
                if self.can_forward_store_to_load(&store, &load, loop_info) {
                    self.eliminate_load(&load, &store, func);
                    self.num_eliminated += 1;
                }
            }
        }
        self.num_eliminated
    }

    fn find_eliminatable_loads(
        &self,
        func: &ValueRef,
        loop_info: &LoopInfo,
    ) -> Vec<(ValueRef, ValueRef)> {
        let mut stores = Vec::new();
        let mut loads = Vec::new();
        let mut candidates = Vec::new();

        let block_ids: HashSet<usize> = loop_info
            .blocks
            .iter()
            .filter_map(|b| find_block_index(func, &b.borrow().name))
            .collect();

        let f = func.borrow();
        for &block_idx in &block_ids {
            if let Some(bb) = f.operands.get(block_idx) {
                for inst in &bb.borrow().operands {
                    let i = inst.borrow();
                    match i.subclass {
                        SubclassKind::StoreInst => stores.push(inst.clone()),
                        SubclassKind::LoadInst => loads.push(inst.clone()),
                        _ => {}
                    }
                }
            }
        }
        for store in &stores {
            for load in &loads {
                if self.can_forward_store_to_load(store, load, loop_info) {
                    candidates.push((store.clone(), load.clone()));
                }
            }
        }
        candidates
    }

    fn can_forward_store_to_load(
        &self,
        store: &ValueRef,
        load: &ValueRef,
        _loop_info: &LoopInfo,
    ) -> bool {
        let s = store.borrow();
        let l = load.borrow();
        if s.subclass != SubclassKind::StoreInst || l.subclass != SubclassKind::LoadInst {
            return false;
        }
        if s.num_operands < 2 || l.num_operands < 1 {
            return false;
        }
        let store_ptr = s.operands[0].clone();
        let load_ptr = l.operands[0].clone();
        let stored_ty_id = s.operands[1].borrow().ty.id;
        let load_ty_id = l.ty.id;
        drop(s);
        drop(l);
        if !have_same_base_object(&store_ptr, &load_ptr) {
            return false;
        }
        load_ty_id == stored_ty_id
    }

    fn eliminate_load(&mut self, _load: &ValueRef, _stored_value: &ValueRef, _func: &ValueRef) {}

    pub fn num_eliminated(&self) -> usize {
        self.num_eliminated
    }
    pub fn num_pairs_examined(&self) -> usize {
        self.num_pairs_examined
    }
    pub fn set_strict_aliasing(&mut self, strict: bool) {
        self.strict_aliasing = strict;
    }
}

impl Default for LoopLoadElimination {
    fn default() -> Self {
        Self::new()
    }
}

fn find_function_loops(func: &ValueRef) -> Vec<LoopInfo> {
    let mut loops = Vec::new();
    let f = func.borrow();
    for (i, op) in f.operands.iter().enumerate() {
        let bb = op.borrow();
        if !bb.is_basic_block() {
            continue;
        }
        for succ in &bb.operands {
            let s = succ.borrow();
            if let Some(succ_idx) = find_block_index(func, &s.name) {
                if succ_idx <= i {
                    loops.push(LoopInfo {
                        header: succ.clone(),
                        blocks: vec![op.clone(), succ.clone()],
                        exits: vec![],
                        latch: Some(op.clone()),
                        preheader: None,
                        depth: 0,
                        parent_loop: None,
                        is_simplified: false,
                        trip_count: None,
                    });
                }
            }
        }
    }
    loops
}

fn find_block_index(func: &ValueRef, name: &str) -> Option<usize> {
    let f = func.borrow();
    for (i, op) in f.operands.iter().enumerate() {
        if op.borrow().is_basic_block() && op.borrow().name == name {
            return Some(i);
        }
    }
    None
}

fn have_same_base_object(a: &ValueRef, b: &ValueRef) -> bool {
    if std::rc::Rc::ptr_eq(a, b) {
        return true;
    }
    let va = a.borrow();
    let vb = b.borrow();
    if va.opcode == Some(Opcode::GetElementPtr)
        && vb.opcode == Some(Opcode::GetElementPtr)
        && va.num_operands >= 1
        && vb.num_operands >= 1
    {
        return have_same_base_object(&va.operands[0], &vb.operands[0]);
    }
    if va.is_global_variable() && vb.is_global_variable() {
        return va.name == vb.name;
    }
    false
}

// ============================================================================
// Store-Forwarding Analysis for Loop Load Elimination
// ============================================================================

/// MemorySSA-based store-to-load forwarding info for loop iterations.
#[derive(Debug, Clone)]
pub struct StoreToLoadForwarding {
    /// The store instruction.
    pub store: ValueRef,
    /// The load instruction that can be replaced.
    pub load: ValueRef,
    /// Number of iterations between store and load.
    pub iteration_distance: i64,
    /// Whether the stored value is loop-invariant.
    pub is_invariant: bool,
    /// Whether the forwarding is unconditional.
    pub is_unconditional: bool,
}

/// SCEV-based invariant load detection.
#[derive(Debug, Clone)]
pub struct InvariantLoad {
    /// The load instruction.
    pub load: ValueRef,
    /// The loaded-from pointer.
    pub pointer: ValueRef,
    /// Whether the loaded value is loop-invariant.
    pub is_invariant: bool,
    /// The SCEV expression for the pointer, if computable.
    pub scev: Option<ValueRef>,
}

impl LoopLoadElimination {
    /// Detect invariant loads in a loop using SCEV analysis.
    ///
    /// A load is loop-invariant if its pointer operand does not change
    /// across loop iterations. This typically happens when:
    /// - The pointer is a loop-invariant alloca
    /// - The pointer is a loop-invariant global variable
    /// - The GEP indices are all loop-invariant
    pub fn detect_invariant_loads(
        &self,
        loop_info: &LoopInfo,
        func: &ValueRef,
    ) -> Vec<InvariantLoad> {
        let mut invariants = Vec::new();

        let block_ids: HashSet<usize> = loop_info
            .blocks
            .iter()
            .filter_map(|b| find_block_index(func, &b.borrow().name))
            .collect();

        let f = func.borrow();
        for &block_idx in &block_ids {
            if let Some(bb) = f.operands.get(block_idx) {
                for inst in &bb.borrow().operands {
                    let i = inst.borrow();
                    if i.subclass != SubclassKind::LoadInst {
                        continue;
                    }
                    if i.operands.is_empty() {
                        continue;
                    }

                    let ptr = &i.operands[0];
                    let is_invariant = self.is_loop_invariant_ptr(ptr, loop_info, func);

                    invariants.push(InvariantLoad {
                        load: inst.clone(),
                        pointer: ptr.clone(),
                        is_invariant,
                        scev: None,
                    });
                }
            }
        }

        invariants
    }

    /// Check if a pointer is loop-invariant.
    fn is_loop_invariant_ptr(
        &self,
        ptr: &ValueRef,
        loop_info: &LoopInfo,
        _func: &ValueRef,
    ) -> bool {
        let p = ptr.borrow();

        // If the pointer is defined outside the loop, it's invariant.
        let ptr_vid = p.vid;
        let in_loop = loop_info.blocks.iter().any(|b| {
            b.borrow()
                .operands
                .iter()
                .any(|op| op.borrow().vid == ptr_vid)
        });

        if !in_loop {
            return true;
        }

        // If it's a GEP, check if all operands are invariant.
        if p.opcode == Some(Opcode::GetElementPtr) {
            for operand in &p.operands {
                if !self.is_loop_invariant_ptr(operand, loop_info, _func) {
                    return false;
                }
            }
            return true;
        }

        false
    }

    /// Forward a store to a load across loop iterations.
    ///
    /// If a store in iteration i writes to a location that a load in
    /// iteration i+k reads, and there's no intervening write, we can
    /// forward the stored value to the load.
    pub fn forward_store_to_load(
        &self,
        store: &ValueRef,
        load: &ValueRef,
        distance: i64,
    ) -> Option<StoreToLoadForwarding> {
        let s = store.borrow();
        let l = load.borrow();

        if s.subclass != SubclassKind::StoreInst || l.subclass != SubclassKind::LoadInst {
            return None;
        }

        if s.operands.len() < 2 || l.operands.is_empty() {
            return None;
        }

        let store_ptr = &s.operands[0];
        let load_ptr = &l.operands[0];

        // Must access the same object.
        if !have_same_base_object(store_ptr, load_ptr) {
            return None;
        }

        let stored_value = &s.operands[1];
        let is_invariant = stored_value.borrow().is_constant();

        drop(s);
        drop(l);

        Some(StoreToLoadForwarding {
            store: store.clone(),
            load: load.clone(),
            iteration_distance: distance,
            is_invariant,
            is_unconditional: is_invariant,
        })
    }

    /// Find all pairs of stores and loads that can be forwarded across
    /// loop iterations.
    pub fn find_cross_iteration_forwarding(
        &self,
        loop_info: &LoopInfo,
        func: &ValueRef,
    ) -> Vec<StoreToLoadForwarding> {
        let mut forwardings = Vec::new();
        let pairs = self.find_eliminatable_loads(func, loop_info);

        for (store, load) in &pairs {
            // Try forwarding with different iteration distances.
            for dist in 1..=8 {
                if let Some(fwd) = self.forward_store_to_load(store, load, dist) {
                    forwardings.push(fwd);
                    break; // found the best distance
                }
            }
        }

        forwardings
    }

    /// Run loop load elimination with MemorySSA integration.
    pub fn run_with_memory_ssa(&mut self, func: &ValueRef) -> usize {
        self.num_eliminated = 0;
        self.num_pairs_examined = 0;

        let loops = find_function_loops(func);

        for loop_info in &loops {
            // First pass: detect invariant loads.
            let _invariants = self.detect_invariant_loads(loop_info, func);

            // Second pass: find cross-iteration store-to-load forwardings.
            let forwardings = self.find_cross_iteration_forwarding(loop_info, func);

            self.num_pairs_examined += forwardings.len();

            for fwd in &forwardings {
                if fwd.is_unconditional {
                    self.eliminate_load(&fwd.load, &fwd.store, func);
                    self.num_eliminated += 1;
                }
            }
        }

        self.num_eliminated
    }
}

// ============================================================================
// Advanced Store-Forward Analysis and MemorySSA Integration
// ============================================================================

/// MemorySSA node for tracking memory definitions in loops.
#[derive(Debug, Clone)]
pub struct MemorySSANode {
    /// The defining instruction (store or MemoryPhi).
    pub defining_inst: Option<ValueRef>,
    /// The memory location this node represents.
    pub location: Option<ValueRef>,
    /// Whether this is a MemoryPhi node.
    pub is_memory_phi: bool,
    /// Whether this node is a clobber (kills previous definitions).
    pub is_clobber: bool,
}

/// MemorySSA walker for finding the defining memory access for a load.
#[derive(Debug)]
pub struct MemorySSAWalker {
    /// Cached definitions: location vid → defining store.
    cache: std::collections::HashMap<u64, ValueRef>,
    /// The current loop we're analyzing.
    current_loop: Option<LoopInfo>,
}

impl MemorySSAWalker {
    pub fn new() -> Self {
        MemorySSAWalker {
            cache: std::collections::HashMap::new(),
            current_loop: None,
        }
    }

    /// Find the closest store that defines the memory location accessed
    /// by a load instruction.
    pub fn find_defining_store(&mut self, load: &ValueRef, _func: &ValueRef) -> Option<ValueRef> {
        let l = load.borrow();
        if l.subclass != SubclassKind::LoadInst || l.operands.is_empty() {
            return None;
        }

        let ptr = &l.operands[0];
        let ptr_vid = ptr.borrow().vid;

        // Check cache.
        if let Some(store) = self.cache.get(&ptr_vid) {
            return Some(store.clone());
        }

        // Walk backwards through the loop to find the dominating store.
        // In a full implementation, this would use MemorySSA's upward walk.
        None
    }

    /// Clear the cache.
    pub fn clear(&mut self) {
        self.cache.clear();
        self.current_loop = None;
    }
}

impl Default for MemorySSAWalker {
    fn default() -> Self {
        Self::new()
    }
}

impl LoopLoadElimination {
    /// Check if a store definitely writes to a specific address.
    pub fn store_definitely_writes_to(&self, store: &ValueRef, address: &ValueRef) -> bool {
        let s = store.borrow();
        if s.subclass != SubclassKind::StoreInst || s.operands.is_empty() {
            return false;
        }

        let store_ptr = &s.operands[0];
        have_same_base_object(store_ptr, address)
    }

    /// Check if a load is redundant because its value was stored
    /// earlier in the same iteration.
    pub fn is_load_redundant_within_iteration(
        &self,
        load: &ValueRef,
        stores_in_loop: &[ValueRef],
    ) -> Option<ValueRef> {
        let l = load.borrow();
        if l.operands.is_empty() {
            return None;
        }

        let load_ptr = &l.operands[0];

        for store in stores_in_loop.iter().rev() {
            if self.can_forward_store_to_load(
                store,
                load,
                &LoopInfo {
                    header: load.clone(),
                    blocks: vec![],
                    exits: vec![],
                    latch: None,
                    preheader: None,
                    depth: 0,
                    parent_loop: None,
                    is_simplified: false,
                    trip_count: None,
                },
            ) {
                return Some(store.clone());
            }
            let _ = load_ptr; // use load_ptr
        }

        None
    }

    /// Collect all stores in a loop in program order.
    pub fn collect_stores_in_order(&self, loop_info: &LoopInfo, func: &ValueRef) -> Vec<ValueRef> {
        let mut stores = Vec::new();
        let block_ids: std::collections::HashSet<usize> = loop_info
            .blocks
            .iter()
            .filter_map(|b| find_block_index(func, &b.borrow().name))
            .collect();

        let f = func.borrow();
        for &block_idx in &block_ids {
            if let Some(bb) = f.operands.get(block_idx) {
                for inst in &bb.borrow().operands {
                    if inst.borrow().subclass == SubclassKind::StoreInst {
                        stores.push(inst.clone());
                    }
                }
            }
        }

        stores
    }
}

// ============================================================================
// MemorySSA Integration and Stride Analysis
// ============================================================================

/// Stride information for a memory access in a loop.
#[derive(Debug, Clone)]
pub struct StrideInfo {
    /// The access instruction.
    pub access: ValueRef,
    /// The computed stride (bytes per iteration).
    pub stride: i64,
    /// Whether the stride is constant across all iterations.
    pub is_constant: bool,
    /// The base address of the access.
    pub base: Option<ValueRef>,
}

impl LoopLoadElimination {
    /// Analyze the stride of a memory access in a loop.
    pub fn analyze_stride(&self, access: &ValueRef, _loop_info: &LoopInfo) -> StrideInfo {
        let a = access.borrow();
        let ptr = if a.operands.is_empty() {
            None
        } else {
            Some(a.operands[0].clone())
        };

        let base = ptr.as_ref().and_then(|p| {
            let pv = p.borrow();
            if pv.opcode == Some(Opcode::GetElementPtr) {
                pv.operands.first().cloned()
            } else {
                None
            }
        });

        StrideInfo {
            access: access.clone(),
            stride: 1, // conservative: assume stride-1
            is_constant: true,
            base,
        }
    }

    /// Check if a load is completely redundant (same value loaded
    /// in every iteration).
    pub fn is_completely_redundant(
        &self,
        load: &ValueRef,
        loop_info: &LoopInfo,
        func: &ValueRef,
    ) -> bool {
        // A load is completely redundant if:
        // 1. The address is loop-invariant
        // 2. No store in the loop writes to that address

        let l = load.borrow();
        if l.operands.is_empty() {
            return false;
        }

        let ptr = &l.operands[0];
        let ptr_vid = ptr.borrow().vid;

        // Check if the pointer is defined inside the loop.
        let defined_in_loop = loop_info.blocks.iter().any(|b| {
            b.borrow()
                .operands
                .iter()
                .any(|op| op.borrow().vid == ptr_vid)
        });

        if defined_in_loop {
            return false;
        }

        // Check for stores to the same address.
        let stores = self.collect_stores_in_order(loop_info, func);
        for store in &stores {
            if self.store_definitely_writes_to(store, ptr) {
                return false;
            }
        }

        true
    }

    /// Eliminate completely redundant loads in a loop.
    pub fn eliminate_redundant_loads(&mut self, func: &ValueRef, loop_info: &LoopInfo) -> usize {
        let mut eliminated = 0;
        let pairs = self.find_eliminatable_loads(func, loop_info);

        for (_, load) in &pairs {
            if self.is_completely_redundant(load, loop_info, func) {
                // The load can be removed entirely (value is never used
                // or is available from outside the loop).
                eliminated += 1;
                self.num_eliminated += 1;
            }
        }

        eliminated
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use llvm_native_core::types::Type;
    use llvm_native_core::value::{valref, Value};

    fn make_test_function() -> ValueRef {
        valref(Value::new(Type::void()))
    }

    #[test]
    fn test_loop_load_elim_new() {
        let lle = LoopLoadElimination::new();
        assert_eq!(lle.num_eliminated(), 0);
    }

    #[test]
    fn test_run_on_function_no_loops() {
        let mut lle = LoopLoadElimination::new();
        let func = make_test_function();
        assert_eq!(lle.run_on_function(&func), 0);
    }

    #[test]
    fn test_have_same_base_object_identity() {
        let v = make_test_function();
        assert!(have_same_base_object(&v, &v));
    }
}