2.4 KiB
LLVM API Cheat Sheet for Mergen Passes
Quick reference for LLVM APIs used in GEPLoadPass, PromotePseudoStackPass, ReplaceTruncWithLoadPass, and PromotePseudoMemory.
-
Value::replaceAllUsesWith(Value* newVal)Replaces every use of this value withnewVal. Does NOT touch this instruction's own operands. Gotcha: After RAUW the instruction is dead but still holds operand references — erase it or it leaks. -
Instruction::eraseFromParent()Removes from the parent BasicBlock ilist and deallocates. Pointer is invalid after the call. Gotcha: Assertsuse_empty(). RAUW first, then erase — never the reverse. -
Value::use_empty()Returns true when nothing references this value. Gotcha: Check AFTER erasing/replacing all users, not before. A stale user makes this return false. -
GetElementPtrInst::getPointerOperand()Returns operand 0 — the base pointer. Forgep i8, ptr %memory, i64 <off>this is%memory. Gotcha: Prefer overgetOperand(getNumOperands()-2)which breaks on multi-index GEPs. -
Type::getIntegerBitWidth()Returns the bit width of aniNtype. Gotcha: Asserts if!isIntegerTy(). Always guard:if (Ty->isIntegerTy()) { ... getBitWidth ... }. -
Type::isIntegerTy()Returns true only foriNtypes. False for float, vector, pointer, void, struct, array. Gotcha: Pointer types are NOT integer types — test before callinggetIntegerBitWidth()orcomputeKnownBits. -
BasicBlock iteration with erasure Pattern:
for (auto it = BB.begin(); it != BB.end();) { auto* I = &*it++; /* may erase I */ }Gotcha:it++must advance BEFORE any erase. A range-for (for (auto &I : BB)) crashes on erasure. -
SmallPtrSet<T*, N>Inline-storage set for pointer deduplication. Use when collecting Instructions for deferred erasure. Gotcha: Inserting the same pointer twice is safe (returns false), but erasing a pointer twice is use-after-free. The set prevents that. -
computeKnownBits(Value*, DataLayout&)Computes known-zero/known-one bit masks via ValueTracking. Gotcha: Asserts on non-integer, non-pointer types. Guard callers withisIntegerTy() || isPointerTy(). -
PreservedAnalyses::none()vs::all()Returnnone()if ANY IR was modified;all()if the pass was a no-op. Gotcha: Returningall()after mutating IR silently poisons cached analyses for downstream passes.