llvm.org GIT mirror llvm / 0a17ddc
[IR] Add llvm.dbg.addr, a control-dependent version of llvm.dbg.declare Summary: This implements the design discussed on llvm-dev for better tracking of variables that live in memory through optimizations: http://lists.llvm.org/pipermail/llvm-dev/2017-September/117222.html This is tracked as PR34136 llvm.dbg.addr is intended to be produced and used in almost precisely the same way as llvm.dbg.declare is today, with the exception that it is control-dependent. That means that dbg.addr should always have a position in the instruction stream, and it will allow passes that optimize memory operations on local variables to insert llvm.dbg.value calls to reflect deleted stores. See SourceLevelDebugging.rst for more details. The main drawback to generating DBG_VALUE machine instrs is that they usually cause LLVM to emit a location list for DW_AT_location. The next step will be to teach DwarfDebug.cpp how to recognize more DBG_VALUE ranges as not needing a location list, and possibly start setting DW_AT_start_offset for variables whose lifetimes begin mid-scope. Reviewers: aprantl, dblaikie, probinson Subscribers: eraman, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D37768 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313825 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Kleckner 2 years ago
16 changed file(s) with 710 addition(s) and 132 deletion(s). Raw diff Collapse all Expand all
170170 ----------------------------
171171
172172 LLVM uses several intrinsic functions (name prefixed with "``llvm.dbg``") to
173 provide debug information at various points in generated code.
174
175 ``llvm.dbg.declare``
173 track source local variables through optimization and code generation.
174
175 ``llvm.dbg.addr``
176176 ^^^^^^^^^^^^^^^^^^^^
177177
178178 .. code-block:: llvm
179179
180 void @llvm.dbg.declare(metadata, metadata, metadata)
181
182 This intrinsic provides information about a local element (e.g., variable). The
183 first argument is metadata holding the alloca for the variable. The second
184 argument is a `local variable `_ containing a
185 description of the variable. The third argument is a `complex expression
186 `_. An `llvm.dbg.declare` instrinsic describes the
187 *location* of a source variable.
180 void @llvm.dbg.addr(metadata, metadata, metadata)
181
182 This intrinsic provides information about a local element (e.g., variable).
183 The first argument is metadata holding the address of variable, typically a
184 static alloca in the function entry block. The second argument is a
185 `local variable `_ containing a description of
186 the variable. The third argument is a `complex expression
187 `_. An `llvm.dbg.addr` intrinsic describes the
188 *address* of a source variable.
188189
189190 .. code-block:: llvm
190191
191192 %i.addr = alloca i32, align 4
192 call void @llvm.dbg.declare(metadata i32* %i.addr, metadata !1, metadata !2), !dbg !3
193 call void @llvm.dbg.addr(metadata i32* %i.addr, metadata !1,
194 metadata !DIExpression()), !dbg !2
193195 !1 = !DILocalVariable(name: "i", ...) ; int i
194 !2 = !DIExpression()
195 !3 = !DILocation(...)
196 !2 = !DILocation(...)
196197 ...
197198 %buffer = alloca [256 x i8], align 8
198199 ; The address of i is buffer+64.
199 call void @llvm.dbg.declare(metadata [256 x i8]* %buffer, metadata !1, metadata !2)
200 !1 = !DILocalVariable(name: "i", ...) ; int i
201 !2 = !DIExpression(DW_OP_plus, 64)
200 call void @llvm.dbg.addr(metadata [256 x i8]* %buffer, metadata !3,
201 metadata !DIExpression(DW_OP_plus, 64)), !dbg !4
202 !3 = !DILocalVariable(name: "i", ...) ; int i
203 !4 = !DILocation(...)
204
205 A frontend should generate exactly one call to ``llvm.dbg.addr`` at the point
206 of declaration of a source variable. Optimization passes that fully promote the
207 variable from memory to SSA values will replace this call with possibly
208 multiple calls to `llvm.dbg.value`. Passes that delete stores are effectively
209 partial promotion, and they will insert a mix of calls to ``llvm.dbg.value``
210 and ``llvm.dbg.addr`` to track the source variable value when it is available.
211 After optimization, there may be multiple calls to ``llvm.dbg.addr`` describing
212 the program points where the variables lives in memory. All calls for the same
213 concrete source variable must agree on the memory location.
214
215
216 ``llvm.dbg.declare``
217 ^^^^^^^^^^^^^^^^^^^^
218
219 .. code-block:: llvm
220
221 void @llvm.dbg.declare(metadata, metadata, metadata)
222
223 This intrinsic is identical to `llvm.dbg.addr`, except that there can only be
224 one call to `llvm.dbg.declare` for a given concrete `local variable
225 `_. It is not control-dependent, meaning that if
226 a call to `llvm.dbg.declare` exists and has a valid location argument, that
227 address is considered to be the true home of the variable across its entire
228 lifetime. This makes it hard for optimizations to preserve accurate debug info
229 in the presence of ``llvm.dbg.declare``, so we are transitioning away from it,
230 and we plan to deprecate it in future LLVM releases.
202231
203232
204233 ``llvm.dbg.value``
240269 7. }
241270 8. X = Y;
242271 9. }
272
273 .. FIXME: Update the following example to use llvm.dbg.addr once that is the
274 default in clang.
243275
244276 Compiled to LLVM, this function would be represented like this:
245277
7070 /// variable's value or its address.
7171 Value *getVariableLocation(bool AllowNullOp = true) const;
7272
73 /// Does this describe the address of a local variable. True for dbg.addr
74 /// and dbg.declare, but not dbg.value, which describes its value.
75 bool isAddressOfVariable() const {
76 return getIntrinsicID() != Intrinsic::dbg_value;
77 }
78
7379 DILocalVariable *getVariable() const {
7480 return cast(getRawVariable());
7581 }
8692 return cast(getArgOperand(2))->getMetadata();
8793 }
8894
89 // Methods for support type inquiry through isa, cast, and dyn_cast:
95 /// \name Casting methods
96 /// @{
9097 static bool classof(const IntrinsicInst *I) {
9198 switch (I->getIntrinsicID()) {
9299 case Intrinsic::dbg_declare:
93100 case Intrinsic::dbg_value:
101 case Intrinsic::dbg_addr:
94102 return true;
95103 default: return false;
96104 }
98106 static bool classof(const Value *V) {
99107 return isa(V) && classof(cast(V));
100108 }
109 /// @}
101110 };
102111
103112 /// This represents the llvm.dbg.declare instruction.
105114 public:
106115 Value *getAddress() const { return getVariableLocation(); }
107116
108 // Methods for support type inquiry through isa, cast, and dyn_cast:
117 /// \name Casting methods
118 /// @{
109119 static bool classof(const IntrinsicInst *I) {
110120 return I->getIntrinsicID() == Intrinsic::dbg_declare;
121 }
122 static bool classof(const Value *V) {
123 return isa(V) && classof(cast(V));
124 }
125 /// @}
126 };
127
128 /// This represents the llvm.dbg.addr instruction.
129 class DbgAddrIntrinsic : public DbgInfoIntrinsic {
130 public:
131 Value *getAddress() const { return getVariableLocation(); }
132
133 /// \name Casting methods
134 /// @{
135 static bool classof(const IntrinsicInst *I) {
136 return I->getIntrinsicID() == Intrinsic::dbg_addr;
111137 }
112138 static bool classof(const Value *V) {
113139 return isa(V) && classof(cast(V));
121147 return getVariableLocation(/* AllowNullOp = */ false);
122148 }
123149
124 // Methods for support type inquiry through isa, cast, and dyn_cast:
150 /// \name Casting methods
151 /// @{
125152 static bool classof(const IntrinsicInst *I) {
126153 return I->getIntrinsicID() == Intrinsic::dbg_value;
127154 }
128155 static bool classof(const Value *V) {
129156 return isa(V) && classof(cast(V));
130157 }
158 /// @}
131159 };
132160
133161 /// This is the common base class for constrained floating point intrinsics.
582582 let IntrProperties = [IntrNoMem, IntrSpeculatable] in {
583583 def int_dbg_declare : Intrinsic<[],
584584 [llvm_metadata_ty,
585 llvm_metadata_ty,
586 llvm_metadata_ty]>;
585 llvm_metadata_ty,
586 llvm_metadata_ty]>;
587587 def int_dbg_value : Intrinsic<[],
588 [llvm_metadata_ty,
589 llvm_metadata_ty,
590 llvm_metadata_ty]>;
591 def int_dbg_addr : Intrinsic<[],
588592 [llvm_metadata_ty,
589593 llvm_metadata_ty,
590594 llvm_metadata_ty]>;
1515 #define LLVM_TRANSFORMS_UTILS_LOCAL_H
1616
1717 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/TinyPtrVector.h"
1819 #include "llvm/Analysis/AliasAnalysis.h"
1920 #include "llvm/IR/DataLayout.h"
2021 #include "llvm/IR/Dominators.h"
3132 class Instruction;
3233 class CallInst;
3334 class DbgDeclareInst;
35 class DbgInfoIntrinsic;
3436 class DbgValueInst;
3537 class StoreInst;
3638 class LoadInst;
261263 ///
262264
263265 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
264 /// that has an associated llvm.dbg.decl intrinsic.
265 void ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
266 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
267 void ConvertDebugDeclareToDebugValue(DbgInfoIntrinsic *DII,
266268 StoreInst *SI, DIBuilder &Builder);
267269
268270 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
269 /// that has an associated llvm.dbg.decl intrinsic.
270 void ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
271 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
272 void ConvertDebugDeclareToDebugValue(DbgInfoIntrinsic *DII,
271273 LoadInst *LI, DIBuilder &Builder);
272274
273 /// Inserts a llvm.dbg.value intrinsic after a phi of an alloca'd value
274 /// that has an associated llvm.dbg.decl intrinsic.
275 void ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
275 /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
276 /// llvm.dbg.declare or llvm.dbg.addr intrinsic.
277 void ConvertDebugDeclareToDebugValue(DbgInfoIntrinsic *DII,
276278 PHINode *LI, DIBuilder &Builder);
277279
278280 /// Lowers llvm.dbg.declare intrinsics into appropriate set of
279281 /// llvm.dbg.value intrinsics.
280282 bool LowerDbgDeclare(Function &F);
281283
282 /// Finds the llvm.dbg.declare intrinsic corresponding to an alloca, if any.
283 DbgDeclareInst *FindAllocaDbgDeclare(Value *V);
284 /// Finds all intrinsics declaring local variables as living in the memory that
285 /// 'V' points to. This may include a mix of dbg.declare and
286 /// dbg.addr intrinsics.
287 TinyPtrVector FindDbgAddrUses(Value *V);
284288
285289 /// Finds the llvm.dbg.value intrinsics describing a value.
286290 void findDbgValues(SmallVectorImpl &DbgValues, Value *V);
51085108 DAG.setRoot(CallResult.second);
51095109 return nullptr;
51105110 }
5111 case Intrinsic::dbg_addr:
51115112 case Intrinsic::dbg_declare: {
5112 const DbgDeclareInst &DI = cast>(I);
5113 const DbgInfoIntrinsic &DI = cast>(I);
51135114 DILocalVariable *Variable = DI.getVariable();
51145115 DIExpression *Expression = DI.getExpression();
5115 const Value *Address = DI.getAddress();
51165116 assert(Variable && "Missing variable");
5117 if (!Address) {
5118 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
5119 return nullptr;
5120 }
51215117
51225118 // Check if address has undef value.
5123 if (isa(Address) ||
5119 const Value *Address = DI.getVariableLocation();
5120 if (!Address || isa(Address) ||
51245121 (Address->use_empty() && !isa(Address))) {
51255122 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
51265123 return nullptr;
51275124 }
51285125
5129 // Static allocas are handled more efficiently in the variable frame index
5130 // side table.
5126 bool isParameter = Variable->isParameter() || isa(Address);
5127
5128 // Check if this variable can be described by a frame index, typically
5129 // either as a static alloca or a byval parameter.
5130 int FI = INT_MAX;
51315131 if (const auto *AI =
5132 dyn_cast(Address->stripInBoundsConstantOffsets()))
5133 if (AI->isStaticAlloca() && FuncInfo.StaticAllocaMap.count(AI))
5134 return nullptr;
5135
5136 // Byval arguments with frame indices were already handled after argument
5137 // lowering and before isel.
5138 if (const auto *Arg =
5139 dyn_cast(Address->stripInBoundsConstantOffsets()))
5140 if (FuncInfo.getArgumentFrameIndex(Arg) != INT_MAX)
5141 return nullptr;
5132 dyn_cast(Address->stripInBoundsConstantOffsets())) {
5133 if (AI->isStaticAlloca()) {
5134 auto I = FuncInfo.StaticAllocaMap.find(AI);
5135 if (I != FuncInfo.StaticAllocaMap.end())
5136 FI = I->second;
5137 }
5138 } else if (const auto *Arg = dyn_cast(
5139 Address->stripInBoundsConstantOffsets())) {
5140 FI = FuncInfo.getArgumentFrameIndex(Arg);
5141 }
5142
5143 // llvm.dbg.addr is control dependent and always generates indirect
5144 // DBG_VALUE instructions. llvm.dbg.declare is handled as a frame index in
5145 // the MachineFunction variable table.
5146 if (FI != INT_MAX) {
5147 if (Intrinsic == Intrinsic::dbg_addr)
5148 DAG.AddDbgValue(DAG.getFrameIndexDbgValue(Variable, Expression, FI, dl,
5149 SDNodeOrder),
5150 getRoot().getNode(), isParameter);
5151 return nullptr;
5152 }
51425153
51435154 SDValue &N = NodeMap[Address];
51445155 if (!N.getNode() && isa(Address))
51495160 if (const BitCastInst *BCI = dyn_cast(Address))
51505161 Address = BCI->getOperand(0);
51515162 // Parameters are handled specially.
5152 bool isParameter = Variable->isParameter() || isa(Address);
51535163 auto FINode = dyn_cast(N.getNode());
51545164 if (isParameter && FINode) {
51555165 // Byval parameter. We have a frame index at this point.
2222
2323 using namespace llvm;
2424 using namespace llvm::dwarf;
25
26 cl::opt
27 UseDbgAddr("use-dbg-addr",
28 llvm::cl::desc("Use llvm.dbg.addr for all local variables"),
29 cl::init(false));
2530
2631 DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes)
2732 : M(m), VMContext(M.getContext()), CUNode(nullptr),
775780 return I;
776781 }
777782
783 static Function *getDeclareIntrin(Module &M) {
784 return Intrinsic::getDeclaration(&M, UseDbgAddr ? Intrinsic::dbg_addr
785 : Intrinsic::dbg_declare);
786 }
787
778788 Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
779789 DIExpression *Expr, const DILocation *DL,
780790 Instruction *InsertBefore) {
784794 VarInfo->getScope()->getSubprogram() &&
785795 "Expected matching subprograms");
786796 if (!DeclareFn)
787 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
797 DeclareFn = getDeclareIntrin(M);
788798
789799 trackIfUnresolved(VarInfo);
790800 trackIfUnresolved(Expr);
803813 VarInfo->getScope()->getSubprogram() &&
804814 "Expected matching subprograms");
805815 if (!DeclareFn)
806 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
816 DeclareFn = getDeclareIntrin(M);
807817
808818 trackIfUnresolved(VarInfo);
809819 trackIfUnresolved(Expr);
40004000 "invalid llvm.dbg.declare intrinsic call 1", CS);
40014001 visitDbgIntrinsic("declare", cast(*CS.getInstruction()));
40024002 break;
4003 case Intrinsic::dbg_addr: // llvm.dbg.addr
4004 visitDbgIntrinsic("addr", cast(*CS.getInstruction()));
40034005 case Intrinsic::dbg_value: // llvm.dbg.value
40044006 visitDbgIntrinsic("value", cast(*CS.getInstruction()));
40054007 break;
21052105
21062106 // If we are removing an alloca with a dbg.declare, insert dbg.value calls
21072107 // before each store.
2108 DbgDeclareInst *DDI = nullptr;
2108 TinyPtrVector DIIs;
21092109 std::unique_ptr DIB;
21102110 if (isa(MI)) {
2111 DDI = FindAllocaDbgDeclare(&MI);
2111 DIIs = FindDbgAddrUses(&MI);
21122112 DIB.reset(new DIBuilder(*MI.getModule(), /*AllowUnresolved=*/false));
21132113 }
21142114
21442144 } else if (isa(I) || isa(I) ||
21452145 isa(I)) {
21462146 replaceInstUsesWith(*I, UndefValue::get(I->getType()));
2147 } else if (DDI && isa(I)) {
2148 ConvertDebugDeclareToDebugValue(DDI, cast(I), *DIB);
2147 } else if (auto *SI = dyn_cast(I)) {
2148 for (auto *DII : DIIs)
2149 ConvertDebugDeclareToDebugValue(DII, SI, *DIB);
21492150 }
21502151 eraseInstFromFunction(*I);
21512152 }
21582159 None, "", II->getParent());
21592160 }
21602161
2161 if (DDI)
2162 eraseInstFromFunction(*DDI);
2162 for (auto *DII : DIIs)
2163 eraseInstFromFunction(*DII);
21632164
21642165 return eraseInstFromFunction(MI);
21652166 }
41014101
41024102 // Migrate debug information from the old alloca to the new alloca(s)
41034103 // and the individual partitions.
4104 if (DbgDeclareInst *DbgDecl = FindAllocaDbgDeclare(&AI)) {
4105 auto *Var = DbgDecl->getVariable();
4106 auto *Expr = DbgDecl->getExpression();
4104 TinyPtrVector DbgDeclares = FindDbgAddrUses(&AI);
4105 if (!DbgDeclares.empty()) {
4106 auto *Var = DbgDeclares.front()->getVariable();
4107 auto *Expr = DbgDeclares.front()->getExpression();
41074108 DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false);
41084109 uint64_t AllocaSize = DL.getTypeSizeInBits(AI.getAllocatedType());
41094110 for (auto Fragment : Fragments) {
41354136 DIExpression::createFragmentExpression(Expr, Start, Size);
41364137 }
41374138
4138 // Remove any existing dbg.declare intrinsic describing the same alloca.
4139 if (DbgDeclareInst *OldDDI = FindAllocaDbgDeclare(Fragment.Alloca))
4140 OldDDI->eraseFromParent();
4139 // Remove any existing intrinsics describing the same alloca.
4140 for (DbgInfoIntrinsic *OldDII : FindDbgAddrUses(Fragment.Alloca))
4141 OldDII->eraseFromParent();
41414142
41424143 DIB.insertDeclare(Fragment.Alloca, Var, FragmentExpr,
4143 DbgDecl->getDebugLoc(), &AI);
4144 DbgDeclares.front()->getDebugLoc(), &AI);
41444145 }
41454146 }
41464147 return Changed;
42574258
42584259 if (AllocaInst *AI = dyn_cast(I)) {
42594260 DeletedAllocas.insert(AI);
4260 if (DbgDeclareInst *DbgDecl = FindAllocaDbgDeclare(AI))
4261 DbgDecl->eraseFromParent();
4261 for (DbgInfoIntrinsic *OldDII : FindDbgAddrUses(AI))
4262 OldDII->eraseFromParent();
42624263 }
42634264
42644265 ++NumDeleted;
10971097 }
10981098
10991099 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
1100 /// that has an associated llvm.dbg.decl intrinsic.
1101 void llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
1100 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
1101 void llvm::ConvertDebugDeclareToDebugValue(DbgInfoIntrinsic *DII,
11021102 StoreInst *SI, DIBuilder &Builder) {
1103 auto *DIVar = DDI->getVariable();
1103 assert(DII->isAddressOfVariable());
1104 auto *DIVar = DII->getVariable();
11041105 assert(DIVar && "Missing variable");
1105 auto *DIExpr = DDI->getExpression();
1106 auto *DIExpr = DII->getExpression();
11061107 Value *DV = SI->getOperand(0);
11071108
11081109 // If an argument is zero extended then use argument directly. The ZExt
11131114 if (SExtInst *SExt = dyn_cast(SI->getOperand(0)))
11141115 ExtendedArg = dyn_cast(SExt->getOperand(0));
11151116 if (ExtendedArg) {
1116 // If this DDI was already describing only a fragment of a variable, ensure
1117 // If this DII was already describing only a fragment of a variable, ensure
11171118 // that fragment is appropriately narrowed here.
11181119 // But if a fragment wasn't used, describe the value as the original
11191120 // argument (rather than the zext or sext) so that it remains described even
11261127 DIExpr->elements_end() - 3);
11271128 Ops.push_back(dwarf::DW_OP_LLVM_fragment);
11281129 Ops.push_back(FragmentOffset);
1129 const DataLayout &DL = DDI->getModule()->getDataLayout();
1130 const DataLayout &DL = DII->getModule()->getDataLayout();
11301131 Ops.push_back(DL.getTypeSizeInBits(ExtendedArg->getType()));
11311132 DIExpr = Builder.createExpression(Ops);
11321133 }
11331134 DV = ExtendedArg;
11341135 }
11351136 if (!LdStHasDebugValue(DIVar, DIExpr, SI))
1136 Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, DDI->getDebugLoc(),
1137 Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, DII->getDebugLoc(),
11371138 SI);
11381139 }
11391140
11401141 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
1141 /// that has an associated llvm.dbg.decl intrinsic.
1142 void llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
1142 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
1143 void llvm::ConvertDebugDeclareToDebugValue(DbgInfoIntrinsic *DII,
11431144 LoadInst *LI, DIBuilder &Builder) {
1144 auto *DIVar = DDI->getVariable();
1145 auto *DIExpr = DDI->getExpression();
1145 auto *DIVar = DII->getVariable();
1146 auto *DIExpr = DII->getExpression();
11461147 assert(DIVar && "Missing variable");
11471148
11481149 if (LdStHasDebugValue(DIVar, DIExpr, LI))
11531154 // preferable to keep tracking both the loaded value and the original
11541155 // address in case the alloca can not be elided.
11551156 Instruction *DbgValue = Builder.insertDbgValueIntrinsic(
1156 LI, DIVar, DIExpr, DDI->getDebugLoc(), (Instruction *)nullptr);
1157 LI, DIVar, DIExpr, DII->getDebugLoc(), (Instruction *)nullptr);
11571158 DbgValue->insertAfter(LI);
11581159 }
11591160
1160 /// Inserts a llvm.dbg.value intrinsic after a phi
1161 /// that has an associated llvm.dbg.decl intrinsic.
1162 void llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
1161 /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
1162 /// llvm.dbg.declare or llvm.dbg.addr intrinsic.
1163 void llvm::ConvertDebugDeclareToDebugValue(DbgInfoIntrinsic *DII,
11631164 PHINode *APN, DIBuilder &Builder) {
1164 auto *DIVar = DDI->getVariable();
1165 auto *DIExpr = DDI->getExpression();
1165 auto *DIVar = DII->getVariable();
1166 auto *DIExpr = DII->getExpression();
11661167 assert(DIVar && "Missing variable");
11671168
11681169 if (PhiHasDebugValue(DIVar, DIExpr, APN))
11751176 // insertion point.
11761177 // FIXME: Insert dbg.value markers in the successors when appropriate.
11771178 if (InsertionPt != BB->end())
1178 Builder.insertDbgValueIntrinsic(APN, DIVar, DIExpr, DDI->getDebugLoc(),
1179 Builder.insertDbgValueIntrinsic(APN, DIVar, DIExpr, DII->getDebugLoc(),
11791180 &*InsertionPt);
11801181 }
11811182
12301231 return true;
12311232 }
12321233
1233 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the
1234 /// alloca 'V', if any.
1235 DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) {
1236 if (auto *L = LocalAsMetadata::getIfExists(V))
1237 if (auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L))
1238 for (User *U : MDV->users())
1239 if (DbgDeclareInst *DDI = dyn_cast(U))
1240 return DDI;
1241
1242 return nullptr;
1234 /// Finds all intrinsics declaring local variables as living in the memory that
1235 /// 'V' points to. This may include a mix of dbg.declare and
1236 /// dbg.addr intrinsics.
1237 TinyPtrVector llvm::FindDbgAddrUses(Value *V) {
1238 auto *L = LocalAsMetadata::getIfExists(V);
1239 if (!L)
1240 return {};
1241 auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L);
1242 if (!MDV)
1243 return {};
1244
1245 TinyPtrVector Declares;
1246 for (User *U : MDV->users()) {
1247 if (auto *DII = dyn_cast(U))
1248 if (DII->isAddressOfVariable())
1249 Declares.push_back(DII);
1250 }
1251
1252 return Declares;
12431253 }
12441254
12451255 void llvm::findDbgValues(SmallVectorImpl &DbgValues, Value *V) {
12501260 DbgValues.push_back(DVI);
12511261 }
12521262
1253
12541263 bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
12551264 Instruction *InsertBefore, DIBuilder &Builder,
12561265 bool Deref, int Offset) {
1257 DbgDeclareInst *DDI = FindAllocaDbgDeclare(Address);
1258 if (!DDI)
1259 return false;
1260 DebugLoc Loc = DDI->getDebugLoc();
1261 auto *DIVar = DDI->getVariable();
1262 auto *DIExpr = DDI->getExpression();
1263 assert(DIVar && "Missing variable");
1264 DIExpr = DIExpression::prepend(DIExpr, Deref, Offset);
1265 // Insert llvm.dbg.declare immediately after the original alloca, and remove
1266 // old llvm.dbg.declare.
1267 Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, InsertBefore);
1268 DDI->eraseFromParent();
1269 return true;
1266 auto DbgAddrs = FindDbgAddrUses(Address);
1267 for (DbgInfoIntrinsic *DII : DbgAddrs) {
1268 DebugLoc Loc = DII->getDebugLoc();
1269 auto *DIVar = DII->getVariable();
1270 auto *DIExpr = DII->getExpression();
1271 assert(DIVar && "Missing variable");
1272 DIExpr = DIExpression::prepend(DIExpr, Deref, Offset);
1273 // Insert llvm.dbg.declare immediately after the original alloca, and remove
1274 // old llvm.dbg.declare.
1275 Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, InsertBefore);
1276 DII->eraseFromParent();
1277 }
1278 return !DbgAddrs.empty();
12701279 }
12711280
12721281 bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
102102 bool OnlyUsedInOneBlock;
103103
104104 Value *AllocaPointerVal;
105 DbgDeclareInst *DbgDeclare;
105 TinyPtrVector DbgDeclares;
106106
107107 void clear() {
108108 DefiningBlocks.clear();
111111 OnlyBlock = nullptr;
112112 OnlyUsedInOneBlock = true;
113113 AllocaPointerVal = nullptr;
114 DbgDeclare = nullptr;
114 DbgDeclares.clear();
115115 }
116116
117117 /// Scan the uses of the specified alloca, filling in the AllocaInfo used
146146 }
147147 }
148148
149 DbgDeclare = FindAllocaDbgDeclare(AI);
149 DbgDeclares = FindDbgAddrUses(AI);
150150 }
151151 };
152152
244244 /// For each alloca, we keep track of the dbg.declare intrinsic that
245245 /// describes it, if any, so that we can convert it to a dbg.value
246246 /// intrinsic if the alloca gets promoted.
247 SmallVector<DbgDeclareInst *, 8> AllocaDbgDeclares;
247 SmallVector<TinyPtrVector, 8> AllocaDbgDeclares;
248248
249249 /// The set of basic blocks the renamer has already visited.
250250 ///
408408
409409 // Record debuginfo for the store and remove the declaration's
410410 // debuginfo.
411 if (DbgDeclareInst *DDI = Info.DbgDeclare) {
411 for (DbgInfoIntrinsic *DII : Info.DbgDeclares) {
412412 DIBuilder DIB(*AI->getModule(), /*AllowUnresolved*/ false);
413 ConvertDebugDeclareToDebugValue(DDI, Info.OnlyStore, DIB);
414 DDI->eraseFromParent();
415 LBI.deleteValue(DDI);
413 ConvertDebugDeclareToDebugValue(DII, Info.OnlyStore, DIB);
414 DII->eraseFromParent();
415 LBI.deleteValue(DII);
416416 }
417417 // Remove the (now dead) store and alloca.
418418 Info.OnlyStore->eraseFromParent();
504504 while (!AI->use_empty()) {
505505 StoreInst *SI = cast(AI->user_back());
506506 // Record debuginfo for the store before removing it.
507 if (DbgDeclareInst *DDI = Info.DbgDeclare) {
507 for (DbgInfoIntrinsic *DII : Info.DbgDeclares) {
508508 DIBuilder DIB(*AI->getModule(), /*AllowUnresolved*/ false);
509 ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
509 ConvertDebugDeclareToDebugValue(DII, SI, DIB);
510510 }
511511 SI->eraseFromParent();
512512 LBI.deleteValue(SI);
516516 LBI.deleteValue(AI);
517517
518518 // The alloca's debuginfo can be removed as well.
519 if (DbgDeclareInst *DDI = Info.DbgDeclare) {
520 DDI->eraseFromParent();
521 LBI.deleteValue(DDI);
519 for (DbgInfoIntrinsic *DII : Info.DbgDeclares) {
520 DII->eraseFromParent();
521 LBI.deleteValue(DII);
522522 }
523523
524524 ++NumLocalPromoted;
586586 }
587587
588588 // Remember the dbg.declare intrinsic describing this alloca, if any.
589 if (Info.DbgDeclare)
590 AllocaDbgDeclares[AllocaNum] = Info.DbgDeclare;
589 if (!Info.DbgDeclares.empty())
590 AllocaDbgDeclares[AllocaNum] = Info.DbgDeclares;
591591
592592 // Keep the reverse mapping of the 'Allocas' array for the rename pass.
593593 AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
665665 }
666666
667667 // Remove alloca's dbg.declare instrinsics from the function.
668 for (DbgDeclareInst *DDI : AllocaDbgDeclares)
669 if (DDI)
670 DDI->eraseFromParent();
668 for (auto &Declares : AllocaDbgDeclares)
669 for (auto *DII : Declares)
670 DII->eraseFromParent();
671671
672672 // Loop over all of the PHI nodes and see if there are any that we can get
673673 // rid of because they merge all of the same incoming values. This can
894894
895895 // The currently active variable for this block is now the PHI.
896896 IncomingVals[AllocaNo] = APN;
897 if (DbgDeclareInst *DDI = AllocaDbgDeclares[AllocaNo])
898 ConvertDebugDeclareToDebugValue(DDI, APN, DIB);
897 for (DbgInfoIntrinsic *DII : AllocaDbgDeclares[AllocaNo])
898 ConvertDebugDeclareToDebugValue(DII, APN, DIB);
899899
900900 // Get the next phi node.
901901 ++PNI;
951951 // what value were we writing?
952952 IncomingVals[ai->second] = SI->getOperand(0);
953953 // Record debuginfo for the store before removing it.
954 if (DbgDeclareInst *DDI = AllocaDbgDeclares[ai->second])
955 ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
954 for (DbgInfoIntrinsic *DII : AllocaDbgDeclares[ai->second])
955 ConvertDebugDeclareToDebugValue(DII, SI, DIB);
956956 BB->getInstList().erase(SI);
957957 }
958958 }
0 ; RUN: llc %s -o %t.s
1 ; RUN: llvm-mc %t.s -filetype=obj -triple=x86_64-windows-msvc -o %t.o
2 ; RUN: FileCheck %s < %t.s --check-prefix=ASM
3 ; RUN: llvm-dwarfdump %t.o | FileCheck %s --check-prefix=DWARF
4
5 ; In this example, the variable lives mostly in memory, but at the point of the
6 ; assignment to global, it lives nowhere, and is described as the constant
7 ; value 1.
8
9 ; C source:
10 ;
11 ; void escape(int *);
12 ; extern int global;
13 ; void f(int x) {
14 ; escape(&x);
15 ; x = 1; // DSE should delete and insert dbg.value(i32 1)
16 ; global = x;
17 ; x = 2; // DSE should insert dbg.addr
18 ; escape(&x);
19 ; }
20
21 ; ModuleID = 'dse.c'
22 source_filename = "dse.c"
23 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
24 target triple = "x86_64-pc-windows-msvc19.0.24215"
25
26 declare void @llvm.dbg.addr(metadata, metadata, metadata) #2
27 declare void @llvm.dbg.value(metadata, metadata, metadata) #2
28 declare void @escape(i32*)
29
30 @global = external global i32, align 4
31
32 ; Function Attrs: nounwind uwtable
33 define void @f(i32 %x) #0 !dbg !8 {
34 entry:
35 %x.addr = alloca i32, align 4
36 store i32 %x, i32* %x.addr, align 4
37 call void @llvm.dbg.addr(metadata i32* %x.addr, metadata !13, metadata !DIExpression()), !dbg !18
38 call void @escape(i32* %x.addr), !dbg !19
39 call void @llvm.dbg.value(metadata i32 1, metadata !13, metadata !DIExpression()), !dbg !20
40 store i32 1, i32* @global, align 4, !dbg !22
41 call void @llvm.dbg.addr(metadata i32* %x.addr, metadata !13, metadata !DIExpression()), !dbg !23
42 store i32 2, i32* %x.addr, align 4, !dbg !23
43 call void @escape(i32* %x.addr), !dbg !24
44 ret void, !dbg !25
45 }
46
47 ; ASM-LABEL: f: # @f
48 ; ASM: movl %ecx, [[OFF_X:[0-9]+]](%rsp)
49 ; ASM: #DEBUG_VALUE: f:x <- [DW_OP_plus_uconst [[OFF_X]]] [%RSP+0]
50 ; ASM: callq escape
51 ; ASM: #DEBUG_VALUE: f:x <- 1
52 ; ASM: movl $1, global(%rip)
53 ; ASM: #DEBUG_VALUE: f:x <- [DW_OP_plus_uconst [[OFF_X]]] [%RSP+0]
54 ; ASM: movl $2, [[OFF_X]](%rsp)
55 ; ASM: callq escape
56 ; ASM: retq
57
58 ; DWARF: DW_TAG_formal_parameter
59 ; DWARF-NEXT: DW_AT_location (0x00000000
60 ; DWARF-NEXT: {{[^:]*}}: DW_OP_breg7 RSP+{{[0-9]+}}
61 ; DWARF-NEXT: {{[^:]*}}: DW_OP_consts +1, DW_OP_stack_value
62 ; DWARF-NEXT: {{[^:]*}}: DW_OP_breg7 RSP+{{[0-9]+}})
63 ; DWARF-NEXT: DW_AT_name ("x")
64
65 attributes #0 = { nounwind uwtable }
66 attributes #2 = { nounwind readnone speculatable }
67
68 !llvm.dbg.cu = !{!0}
69 !llvm.module.flags = !{!3, !4, !5, !6}
70 !llvm.ident = !{!7}
71
72 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
73 !1 = !DIFile(filename: "dse.c", directory: "C:\5Csrc\5Cllvm-project\5Cbuild")
74 !2 = !{}
75 !3 = !{i32 2, !"Dwarf Version", i32 4}
76 !4 = !{i32 2, !"Debug Info Version", i32 3}
77 !5 = !{i32 1, !"wchar_size", i32 2}
78 !6 = !{i32 7, !"PIC Level", i32 2}
79 !7 = !{!"clang version 6.0.0 "}
80 !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 3, type: !9, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !12)
81 !9 = !DISubroutineType(types: !10)
82 !10 = !{null, !11}
83 !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
84 !12 = !{!13}
85 !13 = !DILocalVariable(name: "x", arg: 1, scope: !8, file: !1, line: 3, type: !11)
86 !14 = !{!15, !15, i64 0}
87 !15 = !{!"int", !16, i64 0}
88 !16 = !{!"omnipotent char", !17, i64 0}
89 !17 = !{!"Simple C/C++ TBAA"}
90 !18 = !DILocation(line: 3, column: 12, scope: !8)
91 !19 = !DILocation(line: 4, column: 3, scope: !8)
92 !20 = !DILocation(line: 5, column: 5, scope: !8)
93 !21 = !DILocation(line: 6, column: 12, scope: !8)
94 !22 = !DILocation(line: 6, column: 10, scope: !8)
95 !23 = !DILocation(line: 7, column: 5, scope: !8)
96 !24 = !DILocation(line: 8, column: 3, scope: !8)
97 !25 = !DILocation(line: 9, column: 1, scope: !8)
0 ; RUN: llc %s -o %t.s
1 ; RUN: llvm-mc -triple x86_64--linux %t.s -filetype=obj -o %t.o
2 ; RUN: FileCheck < %t.s %s
3 ; RUN: llvm-dwarfdump %t.o | FileCheck %s --check-prefix=DWARF
4
5 ; Unlike dbg.declare, dbg.addr should be lowered to DBG_VALUE instructions. It
6 ; is control-dependent.
7
8 ; CHECK-LABEL: use_dbg_addr:
9 ; CHECK: #DEBUG_VALUE: use_dbg_addr:o <- [%RSP+0]
10
11 ; FIXME: Avoid the use of a single-location location list and use
12 ; DW_AT_start_offset instead.
13
14 ; DWARF: DW_TAG_variable
15 ; DWARF-NEXT: DW_AT_location (0x00000000
16 ; DWARF-NEXT: 0x{{.*}} - 0x{{.*}}: DW_OP_breg7 RSP+0)
17 ; DWARF-NEXT: DW_AT_name ("o")
18
19
20 ; ModuleID = 't.c'
21 source_filename = "t.c"
22 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
23 target triple = "x86_64--linux"
24
25 %struct.Foo = type { i32 }
26
27 ; Function Attrs: noinline nounwind uwtable
28 define void @use_dbg_addr() #0 !dbg !7 {
29 entry:
30 %o = alloca %struct.Foo, align 4
31 call void @llvm.dbg.addr(metadata %struct.Foo* %o, metadata !10, metadata !15), !dbg !16
32 call void @escape_foo(%struct.Foo* %o), !dbg !17
33 ret void, !dbg !18
34 }
35
36 ; Function Attrs: nounwind readnone speculatable
37 declare void @llvm.dbg.addr(metadata, metadata, metadata) #1
38
39 declare void @escape_foo(%struct.Foo*)
40
41 attributes #0 = { noinline nounwind uwtable }
42 attributes #1 = { nounwind readnone speculatable }
43
44 !llvm.dbg.cu = !{!0}
45 !llvm.module.flags = !{!3, !4, !5}
46 !llvm.ident = !{!6}
47
48 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
49 !1 = !DIFile(filename: "t.c", directory: "C:\5Csrc\5Cllvm-project\5Cbuild")
50 !2 = !{}
51 !3 = !{i32 2, !"Dwarf Version", i32 4}
52 !4 = !{i32 2, !"Debug Info Version", i32 3}
53 !5 = !{i32 1, !"wchar_size", i32 4}
54 !6 = !{!"clang version 6.0.0 "}
55 !7 = distinct !DISubprogram(name: "use_dbg_addr", scope: !1, file: !1, line: 3, type: !8, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
56 !8 = !DISubroutineType(types: !9)
57 !9 = !{null}
58 !10 = !DILocalVariable(name: "o", scope: !7, file: !1, line: 4, type: !11)
59 !11 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Foo", file: !1, line: 1, size: 32, elements: !12)
60 !12 = !{!13}
61 !13 = !DIDerivedType(tag: DW_TAG_member, name: "x", scope: !11, file: !1, line: 1, baseType: !14, size: 32)
62 !14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
63 !15 = !DIExpression()
64 !16 = !DILocation(line: 4, column: 14, scope: !7)
65 !17 = !DILocation(line: 5, column: 3, scope: !7)
66 !18 = !DILocation(line: 6, column: 1, scope: !7)
0 ; RUN: opt -mem2reg -S < %s | FileCheck %s -implicit-check-not="call void @llvm.dbg.addr"
1
2 ; This example is intended to simulate this pass pipeline, which may not exist
3 ; in practice:
4 ; 1. DSE f from the original C source
5 ; 2. Inline escape
6 ; 3. mem2reg
7 ; This exercises the corner case of multiple llvm.dbg.addr intrinsics.
8
9 ; C source:
10 ;
11 ; void escape(int *px) { ++*px; }
12 ; extern int global;
13 ; void f(int x) {
14 ; escape(&x);
15 ; x = 1; // DSE should delete and insert dbg.value(i32 1)
16 ; global = x;
17 ; x = 2; // DSE should insert dbg.addr
18 ; escape(&x);
19 ; }
20
21 ; ModuleID = 'dse.c'
22 source_filename = "dse.c"
23 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
24 target triple = "x86_64-pc-windows-msvc19.0.24215"
25
26 declare void @llvm.dbg.addr(metadata, metadata, metadata) #2
27 declare void @llvm.dbg.value(metadata, metadata, metadata) #2
28
29 @global = external global i32, align 4
30
31 ; Function Attrs: nounwind uwtable
32 define void @f(i32 %x) #0 !dbg !8 {
33 entry:
34 %x.addr = alloca i32, align 4
35 store i32 %x, i32* %x.addr, align 4
36 call void @llvm.dbg.addr(metadata i32* %x.addr, metadata !13, metadata !DIExpression()), !dbg !18
37 %ld.1 = load i32, i32* %x.addr, align 4, !dbg !19
38 %inc.1 = add nsw i32 %ld.1, 1, !dbg !19
39 store i32 %inc.1, i32* %x.addr, align 4, !dbg !19
40 call void @llvm.dbg.value(metadata i32 1, metadata !13, metadata !DIExpression()), !dbg !20
41 store i32 1, i32* @global, align 4, !dbg !22
42 call void @llvm.dbg.addr(metadata i32* %x.addr, metadata !13, metadata !DIExpression()), !dbg !23
43 store i32 2, i32* %x.addr, align 4, !dbg !23
44 %ld.2 = load i32, i32* %x.addr, align 4, !dbg !19
45 %inc.2 = add nsw i32 %ld.2, 1, !dbg !19
46 store i32 %inc.2, i32* %x.addr, align 4, !dbg !19
47 ret void, !dbg !25
48 }
49
50 ; CHECK-LABEL: define void @f(i32 %x)
51 ; CHECK: call void @llvm.dbg.value(metadata i32 %x, metadata !13, metadata !DIExpression())
52 ; CHECK: %inc.1 = add nsw i32 %x, 1
53 ; CHECK: call void @llvm.dbg.value(metadata i32 %inc.1, metadata !13, metadata !DIExpression())
54 ; CHECK: call void @llvm.dbg.value(metadata i32 1, metadata !13, metadata !DIExpression())
55 ; CHECK: store i32 1, i32* @global, align 4
56 ; CHECK: call void @llvm.dbg.value(metadata i32 2, metadata !13, metadata !DIExpression())
57 ; CHECK: %inc.2 = add nsw i32 2, 1
58 ; CHECK: call void @llvm.dbg.value(metadata i32 %inc.2, metadata !13, metadata !DIExpression())
59 ; CHECK: ret void
60
61 attributes #0 = { nounwind uwtable }
62 attributes #2 = { nounwind readnone speculatable }
63
64 !llvm.dbg.cu = !{!0}
65 !llvm.module.flags = !{!3, !4, !5, !6}
66 !llvm.ident = !{!7}
67
68 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
69 !1 = !DIFile(filename: "dse.c", directory: "C:\5Csrc\5Cllvm-project\5Cbuild")
70 !2 = !{}
71 !3 = !{i32 2, !"Dwarf Version", i32 4}
72 !4 = !{i32 2, !"Debug Info Version", i32 3}
73 !5 = !{i32 1, !"wchar_size", i32 2}
74 !6 = !{i32 7, !"PIC Level", i32 2}
75 !7 = !{!"clang version 6.0.0 "}
76 !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 3, type: !9, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !12)
77 !9 = !DISubroutineType(types: !10)
78 !10 = !{null, !11}
79 !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
80 !12 = !{!13}
81 !13 = !DILocalVariable(name: "x", arg: 1, scope: !8, file: !1, line: 3, type: !11)
82 !14 = !{!15, !15, i64 0}
83 !15 = !{!"int", !16, i64 0}
84 !16 = !{!"omnipotent char", !17, i64 0}
85 !17 = !{!"Simple C/C++ TBAA"}
86 !18 = !DILocation(line: 3, column: 12, scope: !8)
87 !19 = !DILocation(line: 4, column: 3, scope: !8)
88 !20 = !DILocation(line: 5, column: 5, scope: !8)
89 !21 = !DILocation(line: 6, column: 12, scope: !8)
90 !22 = !DILocation(line: 6, column: 10, scope: !8)
91 !23 = !DILocation(line: 7, column: 5, scope: !8)
92 !24 = !DILocation(line: 8, column: 3, scope: !8)
93 !25 = !DILocation(line: 9, column: 1, scope: !8)
0 ; RUN: opt -mem2reg -S < %s | FileCheck %s
1
2 ; ModuleID = 'newvars.c'
3 source_filename = "newvars.c"
4 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
5 target triple = "x86_64-pc-windows-msvc19.0.24215"
6
7 ; Function Attrs: nounwind uwtable
8 define i32 @if_else(i32 %cond, i32 %a, i32 %b) !dbg !8 {
9 entry:
10 %x = alloca i32, align 4
11 call void @llvm.dbg.addr(metadata i32* %x, metadata !16, metadata !DIExpression()), !dbg !26
12 store i32 %a, i32* %x, align 4, !dbg !26, !tbaa !17
13 %tobool = icmp ne i32 %cond, 0, !dbg !28
14 br i1 %tobool, label %if.then, label %if.else, !dbg !30
15
16 if.then: ; preds = %entry
17 store i32 0, i32* %x, align 4, !dbg !31, !tbaa !17
18 br label %if.end, !dbg !33
19
20 if.else: ; preds = %entry
21 store i32 %b, i32* %x, align 4, !dbg !36, !tbaa !17
22 br label %if.end
23
24 if.end: ; preds = %if.else, %if.then
25 %rv = load i32, i32* %x, align 4, !dbg !37, !tbaa !17
26 ret i32 %rv, !dbg !39
27 }
28
29 ; CHECK-LABEL: define i32 @if_else({{.*}})
30 ; CHECK: entry:
31 ; CHECK-NOT: alloca i32
32 ; CHECK: call void @llvm.dbg.value(metadata i32 %a, metadata ![[X_LOCAL:[0-9]+]], metadata !DIExpression())
33 ; CHECK: if.then: ; preds = %entry
34 ; CHECK: call void @llvm.dbg.value(metadata i32 0, metadata ![[X_LOCAL]], metadata !DIExpression())
35 ; CHECK: if.else: ; preds = %entry
36 ; CHECK: call void @llvm.dbg.value(metadata i32 %b, metadata ![[X_LOCAL]], metadata !DIExpression())
37 ; CHECK: if.end: ; preds = %if.else, %if.then
38 ; CHECK: %[[PHI:[^ ]*]] = phi i32 [ 0, %if.then ], [ %b, %if.else ]
39 ; CHECK: call void @llvm.dbg.value(metadata i32 %[[PHI]], metadata ![[X_LOCAL]], metadata !DIExpression())
40 ; CHECK: ret i32
41
42 ; CHECK: ![[X_LOCAL]] = !DILocalVariable(name: "x", {{.*}})
43
44 ; Function Attrs: nounwind readnone speculatable
45 declare void @llvm.dbg.declare(metadata, metadata, metadata)
46 declare void @llvm.dbg.addr(metadata, metadata, metadata)
47
48 !llvm.dbg.cu = !{!0}
49 !llvm.module.flags = !{!3, !4, !5, !6}
50 !llvm.ident = !{!7}
51
52 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
53 !1 = !DIFile(filename: "newvars.c", directory: "C:\5Csrc\5Cllvm-project\5Cbuild")
54 !2 = !{}
55 !3 = !{i32 2, !"Dwarf Version", i32 4}
56 !4 = !{i32 2, !"Debug Info Version", i32 3}
57 !5 = !{i32 1, !"wchar_size", i32 2}
58 !6 = !{i32 7, !"PIC Level", i32 2}
59 !7 = !{!"clang version 6.0.0 "}
60 !8 = distinct !DISubprogram(name: "if_else", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !12)
61 !9 = !DISubroutineType(types: !10)
62 !10 = !{!11, !11, !11, !11}
63 !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
64 !12 = !{!13, !14, !15, !16}
65 !13 = !DILocalVariable(name: "b", arg: 3, scope: !8, file: !1, line: 1, type: !11)
66 !14 = !DILocalVariable(name: "a", arg: 2, scope: !8, file: !1, line: 1, type: !11)
67 !15 = !DILocalVariable(name: "cond", arg: 1, scope: !8, file: !1, line: 1, type: !11)
68 !16 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !11)
69 !17 = !{!18, !18, i64 0}
70 !18 = !{!"int", !19, i64 0}
71 !19 = !{!"omnipotent char", !20, i64 0}
72 !20 = !{!"Simple C/C++ TBAA"}
73 !22 = !DILocation(line: 1, column: 34, scope: !8)
74 !23 = !DILocation(line: 1, column: 27, scope: !8)
75 !24 = !DILocation(line: 1, column: 17, scope: !8)
76 !25 = !DILocation(line: 2, column: 3, scope: !8)
77 !26 = !DILocation(line: 2, column: 7, scope: !8)
78 !27 = !DILocation(line: 2, column: 11, scope: !8)
79 !28 = !DILocation(line: 3, column: 7, scope: !29)
80 !29 = distinct !DILexicalBlock(scope: !8, file: !1, line: 3, column: 7)
81 !30 = !DILocation(line: 3, column: 7, scope: !8)
82 !31 = !DILocation(line: 4, column: 7, scope: !32)
83 !32 = distinct !DILexicalBlock(scope: !29, file: !1, line: 3, column: 13)
84 !33 = !DILocation(line: 5, column: 3, scope: !32)
85 !34 = !DILocation(line: 6, column: 9, scope: !35)
86 !35 = distinct !DILexicalBlock(scope: !29, file: !1, line: 5, column: 10)
87 !36 = !DILocation(line: 6, column: 7, scope: !35)
88 !37 = !DILocation(line: 8, column: 10, scope: !8)
89 !38 = !DILocation(line: 9, column: 1, scope: !8)
90 !39 = !DILocation(line: 8, column: 3, scope: !8)
0 ; RUN: opt -use-dbg-addr -sroa -S < %s | FileCheck %s
1
2 ; ModuleID = ''
3 source_filename = "newvars.c"
4 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
5 target triple = "x86_64-pc-windows-msvc19.0.24215"
6
7 %struct.Pair = type { i32, i32 }
8
9 @pair = internal global %struct.Pair zeroinitializer
10
11 ; Function Attrs: nounwind uwtable
12 define void @if_else(i32 %cond, i32 %a, i32 %b) !dbg !8 {
13 entry:
14 %p = alloca %struct.Pair, align 4
15 %0 = bitcast %struct.Pair* %p to i8*, !dbg !25
16 call void @llvm.dbg.addr(metadata %struct.Pair* %p, metadata !20, metadata !DIExpression()), !dbg !26
17 %x = getelementptr inbounds %struct.Pair, %struct.Pair* %p, i32 0, i32 0, !dbg !27
18 store i32 %a, i32* %x, align 4, !dbg !28
19 %y = getelementptr inbounds %struct.Pair, %struct.Pair* %p, i32 0, i32 1, !dbg !34
20 store i32 %b, i32* %y, align 4, !dbg !35
21 %tobool = icmp ne i32 %cond, 0, !dbg !37
22 br i1 %tobool, label %if.then, label %if.else, !dbg !39
23
24 if.then: ; preds = %entry
25 %x1 = getelementptr inbounds %struct.Pair, %struct.Pair* %p, i32 0, i32 0, !dbg !40
26 store i32 0, i32* %x1, align 4, !dbg !42
27 %y2 = getelementptr inbounds %struct.Pair, %struct.Pair* %p, i32 0, i32 1, !dbg !43
28 store i32 %a, i32* %y2, align 4, !dbg !44
29 br label %if.end, !dbg !45
30
31 if.else: ; preds = %entry
32 %x3 = getelementptr inbounds %struct.Pair, %struct.Pair* %p, i32 0, i32 0, !dbg !46
33 store i32 %b, i32* %x3, align 4, !dbg !48
34 %y4 = getelementptr inbounds %struct.Pair, %struct.Pair* %p, i32 0, i32 1, !dbg !49
35 store i32 0, i32* %y4, align 4, !dbg !50
36 br label %if.end
37
38 if.end: ; preds = %if.else, %if.then
39 %1 = bitcast %struct.Pair* %p to i8*, !dbg !51
40 %2 = bitcast %struct.Pair* @pair to i8*, !dbg !51
41 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %2, i8* %1, i64 8, i32 4, i1 false), !dbg !51
42 ret void
43 }
44
45 ; CHECK-LABEL: define void @if_else(i32 %cond, i32 %a, i32 %b)
46 ; CHECK: entry:
47 ; CHECK: call void @llvm.dbg.value(metadata i32 %a, metadata ![[PVAR:[0-9]+]], metadata ![[XFRAG:DIExpression\(DW_OP_LLVM_fragment, 0, 32\)]])
48 ; CHECK: call void @llvm.dbg.value(metadata i32 %b, metadata ![[PVAR]], metadata ![[YFRAG:DIExpression\(DW_OP_LLVM_fragment, 32, 32\)]])
49 ; CHECK: if.then:
50 ; CHECK: call void @llvm.dbg.value(metadata i32 0, metadata ![[PVAR]], metadata ![[XFRAG]])
51 ; CHECK: call void @llvm.dbg.value(metadata i32 %a, metadata ![[PVAR]], metadata ![[YFRAG]])
52 ; CHECK: if.else:
53 ; CHECK: call void @llvm.dbg.value(metadata i32 %b, metadata ![[PVAR]], metadata ![[XFRAG]])
54 ; CHECK: call void @llvm.dbg.value(metadata i32 0, metadata ![[PVAR]], metadata ![[YFRAG]])
55 ; CHECK: if.end:
56 ; CHECK: %p.sroa.4.0 = phi i32 [ %a, %if.then ], [ 0, %if.else ]
57 ; CHECK: %p.sroa.0.0 = phi i32 [ 0, %if.then ], [ %b, %if.else ]
58 ; CHECK: call void @llvm.dbg.value(metadata i32 %p.sroa.0.0, metadata ![[PVAR]], metadata ![[XFRAG]])
59 ; CHECK: call void @llvm.dbg.value(metadata i32 %p.sroa.4.0, metadata ![[PVAR]], metadata ![[YFRAG]])
60
61 ; CHECK: ![[PVAR]] = !DILocalVariable(name: "p", {{.*}})
62
63 ; Function Attrs: argmemonly nounwind
64 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i32, i1) #2
65
66 ; Function Attrs: nounwind readnone speculatable
67 declare void @llvm.dbg.addr(metadata, metadata, metadata)
68
69 !llvm.dbg.cu = !{!0}
70 !llvm.module.flags = !{!3, !4, !5, !6}
71 !llvm.ident = !{!7}
72
73 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
74 !1 = !DIFile(filename: "newvars.c", directory: "C:\5Csrc\5Cllvm-project\5Cbuild")
75 !2 = !{}
76 !3 = !{i32 2, !"Dwarf Version", i32 4}
77 !4 = !{i32 2, !"Debug Info Version", i32 3}
78 !5 = !{i32 1, !"wchar_size", i32 2}
79 !6 = !{i32 7, !"PIC Level", i32 2}
80 !7 = !{!"clang version 6.0.0 "}
81 !8 = distinct !DISubprogram(name: "if_else", scope: !1, file: !1, line: 2, type: !9, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !16)
82 !9 = !DISubroutineType(types: !10)
83 !10 = !{!11, !14, !14, !14}
84 !11 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Pair", file: !1, line: 1, size: 64, elements: !12)
85 !12 = !{!13, !15}
86 !13 = !DIDerivedType(tag: DW_TAG_member, name: "x", scope: !11, file: !1, line: 1, baseType: !14, size: 32)
87 !14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
88 !15 = !DIDerivedType(tag: DW_TAG_member, name: "y", scope: !11, file: !1, line: 1, baseType: !14, size: 32, offset: 32)
89 !16 = !{!17, !18, !19, !20}
90 !17 = !DILocalVariable(name: "b", arg: 3, scope: !8, file: !1, line: 2, type: !14)
91 !18 = !DILocalVariable(name: "a", arg: 2, scope: !8, file: !1, line: 2, type: !14)
92 !19 = !DILocalVariable(name: "cond", arg: 1, scope: !8, file: !1, line: 2, type: !14)
93 !20 = !DILocalVariable(name: "p", scope: !8, file: !1, line: 3, type: !11)
94 !22 = !DILocation(line: 2, column: 42, scope: !8)
95 !23 = !DILocation(line: 2, column: 35, scope: !8)
96 !24 = !DILocation(line: 2, column: 25, scope: !8)
97 !25 = !DILocation(line: 3, column: 3, scope: !8)
98 !26 = !DILocation(line: 3, column: 15, scope: !8)
99 !27 = !DILocation(line: 4, column: 5, scope: !8)
100 !28 = !DILocation(line: 4, column: 7, scope: !8)
101 !29 = !{!30, !31, i64 0}
102 !30 = !{!"Pair", !31, i64 0, !31, i64 4}
103 !31 = !{!"int", !32, i64 0}
104 !32 = !{!"omnipotent char", !33, i64 0}
105 !33 = !{!"Simple C/C++ TBAA"}
106 !34 = !DILocation(line: 5, column: 5, scope: !8)
107 !35 = !DILocation(line: 5, column: 7, scope: !8)
108 !36 = !{!30, !31, i64 4}
109 !37 = !DILocation(line: 6, column: 7, scope: !38)
110 !38 = distinct !DILexicalBlock(scope: !8, file: !1, line: 6, column: 7)
111 !39 = !DILocation(line: 6, column: 7, scope: !8)
112 !40 = !DILocation(line: 7, column: 7, scope: !41)
113 !41 = distinct !DILexicalBlock(scope: !38, file: !1, line: 6, column: 13)
114 !42 = !DILocation(line: 7, column: 9, scope: !41)
115 !43 = !DILocation(line: 8, column: 7, scope: !41)
116 !44 = !DILocation(line: 8, column: 9, scope: !41)
117 !45 = !DILocation(line: 9, column: 3, scope: !41)
118 !46 = !DILocation(line: 10, column: 7, scope: !47)
119 !47 = distinct !DILexicalBlock(scope: !38, file: !1, line: 9, column: 10)
120 !48 = !DILocation(line: 10, column: 9, scope: !47)
121 !49 = !DILocation(line: 11, column: 7, scope: !47)
122 !50 = !DILocation(line: 11, column: 9, scope: !47)
123 !51 = !DILocation(line: 13, column: 10, scope: !8)
124 !52 = !{i64 0, i64 4, !53, i64 4, i64 4, !53}
125 !53 = !{!31, !31, i64 0}
126 !54 = !DILocation(line: 14, column: 1, scope: !8)