blob: cd476a36015502fda55bd2a87a7badf55eb3ec11 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Instruction.cpp - Implement the Instruction class -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00008//
Chandler Carruthef860a22013-01-02 09:10:48 +00009// This file implements the Instruction class for the IR library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000010//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/Instruction.h"
Vedant Kumarf01827f2018-06-19 23:42:17 +000014#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/ADT/DenseSet.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/Instructions.h"
Dehao Chene5930492017-03-20 16:40:44 +000018#include "llvm/IR/MDBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Operator.h"
20#include "llvm/IR/Type.h"
Chris Lattner0e03ab62003-11-20 17:45:12 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattner229907c2011-07-18 04:54:35 +000023Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner2195fc42007-02-24 00:55:48 +000024 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +000025 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
Chris Lattner3c787442002-09-10 15:45:53 +000026
27 // If requested, insert this instruction into a basic block...
28 if (InsertBefore) {
Yaron Keren43b4d382015-06-15 17:03:35 +000029 BasicBlock *BB = InsertBefore->getParent();
30 assert(BB && "Instruction to insert before is not in a basic block!");
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +000031 BB->getInstList().insert(InsertBefore->getIterator(), this);
Chris Lattner3c787442002-09-10 15:45:53 +000032 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000033}
34
Chris Lattner229907c2011-07-18 04:54:35 +000035Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Chris Lattner2195fc42007-02-24 00:55:48 +000036 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +000037 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
Alkis Evlogimenos9f0fdf72004-05-26 21:41:09 +000038
39 // append this instruction into the basic block
40 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
41 InsertAtEnd->getInstList().push_back(this);
Chris Lattner0f048162007-02-13 07:54:42 +000042}
43
Gordon Henriksen14a55692007-12-10 02:14:30 +000044Instruction::~Instruction() {
Craig Topper2617dcc2014-04-15 06:32:26 +000045 assert(!Parent && "Instruction still linked in the program!");
Dan Gohman48a995f2010-07-20 22:25:04 +000046 if (hasMetadataHashEntry())
47 clearMetadataHashEntries();
Chris Lattner1c12a882006-06-21 16:53:47 +000048}
49
50
Chris Lattner9ed7aef2002-09-06 21:33:15 +000051void Instruction::setParent(BasicBlock *P) {
52 Parent = P;
53}
54
Mehdi Amini9a9738f2015-03-03 22:01:13 +000055const Module *Instruction::getModule() const {
56 return getParent()->getModule();
57}
58
Sanjoy Das411fdcd2015-12-08 00:13:12 +000059const Function *Instruction::getFunction() const {
60 return getParent()->getParent();
61}
Philip Reames388402452015-05-26 21:03:23 +000062
Chris Lattner02a71e72004-10-11 22:21:39 +000063void Instruction::removeFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +000064 getParent()->getInstList().remove(getIterator());
Chris Lattner02a71e72004-10-11 22:21:39 +000065}
66
Daniel Berlina5af7202015-04-02 00:03:07 +000067iplist<Instruction>::iterator Instruction::eraseFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +000068 return getParent()->getInstList().erase(getIterator());
Chris Lattner02a71e72004-10-11 22:21:39 +000069}
Vikram S. Adve8aee7962002-07-14 23:09:40 +000070
Sanjay Patelf5c2d122016-01-06 00:18:29 +000071/// Insert an unlinked instruction into a basic block immediately before the
72/// specified instruction.
Owen Anderson2505e0c2008-06-17 18:29:27 +000073void Instruction::insertBefore(Instruction *InsertPos) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +000074 InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
Owen Anderson2505e0c2008-06-17 18:29:27 +000075}
76
Sanjay Patelf5c2d122016-01-06 00:18:29 +000077/// Insert an unlinked instruction into a basic block immediately after the
78/// specified instruction.
Chris Lattner6e66d0a2009-01-13 07:43:51 +000079void Instruction::insertAfter(Instruction *InsertPos) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +000080 InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
81 this);
Chris Lattner6e66d0a2009-01-13 07:43:51 +000082}
83
Sanjay Patelf5c2d122016-01-06 00:18:29 +000084/// Unlink this instruction from its current basic block and insert it into the
85/// basic block that MovePos lives in, right before MovePos.
Chris Lattner24a0a432005-08-08 05:21:50 +000086void Instruction::moveBefore(Instruction *MovePos) {
Duncan P. N. Exon Smith362d12042016-08-17 01:54:41 +000087 moveBefore(*MovePos->getParent(), MovePos->getIterator());
88}
89
Sanjay Patel674d2c22017-08-29 14:07:48 +000090void Instruction::moveAfter(Instruction *MovePos) {
91 moveBefore(*MovePos->getParent(), ++MovePos->getIterator());
92}
93
Duncan P. N. Exon Smith362d12042016-08-17 01:54:41 +000094void Instruction::moveBefore(BasicBlock &BB,
95 SymbolTableList<Instruction>::iterator I) {
96 assert(I == BB.end() || I->getParent() == &BB);
97 BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
Chris Lattner24a0a432005-08-08 05:21:50 +000098}
99
Reid Kleckner0c2b09a2020-02-18 14:33:54 -0800100bool Instruction::comesBefore(const Instruction *Other) const {
101 assert(Parent && Other->Parent &&
102 "instructions without BB parents have no order");
103 assert(Parent == Other->Parent && "cross-BB instruction order comparison");
104 if (!Parent->isInstrOrderValid())
105 Parent->renumberInstructions();
106 return Order < Other->Order;
107}
108
David Majnemer9554c132016-04-22 06:37:45 +0000109void Instruction::setHasNoUnsignedWrap(bool b) {
110 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
111}
112
113void Instruction::setHasNoSignedWrap(bool b) {
114 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
115}
116
117void Instruction::setIsExact(bool b) {
118 cast<PossiblyExactOperator>(this)->setIsExact(b);
119}
120
121bool Instruction::hasNoUnsignedWrap() const {
122 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
123}
124
125bool Instruction::hasNoSignedWrap() const {
126 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
127}
128
Sanjoy Dasaa722ae2017-02-23 22:50:52 +0000129void Instruction::dropPoisonGeneratingFlags() {
130 switch (getOpcode()) {
131 case Instruction::Add:
132 case Instruction::Sub:
133 case Instruction::Mul:
134 case Instruction::Shl:
135 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false);
136 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false);
137 break;
138
139 case Instruction::UDiv:
140 case Instruction::SDiv:
141 case Instruction::AShr:
142 case Instruction::LShr:
143 cast<PossiblyExactOperator>(this)->setIsExact(false);
144 break;
145
146 case Instruction::GetElementPtr:
147 cast<GetElementPtrInst>(this)->setIsInBounds(false);
148 break;
149 }
Philip Reamesf47a3132019-07-09 18:49:29 +0000150 // TODO: FastMathFlags!
Sanjoy Dasaa722ae2017-02-23 22:50:52 +0000151}
152
Philip Reamesf47a3132019-07-09 18:49:29 +0000153
David Majnemer9554c132016-04-22 06:37:45 +0000154bool Instruction::isExact() const {
155 return cast<PossiblyExactOperator>(this)->isExact();
156}
157
Sanjay Patel629c4112017-11-06 16:27:15 +0000158void Instruction::setFast(bool B) {
Michael Ilseman149209e2012-11-27 00:41:22 +0000159 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
Sanjay Patel629c4112017-11-06 16:27:15 +0000160 cast<FPMathOperator>(this)->setFast(B);
161}
162
163void Instruction::setHasAllowReassoc(bool B) {
164 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
165 cast<FPMathOperator>(this)->setHasAllowReassoc(B);
Michael Ilseman149209e2012-11-27 00:41:22 +0000166}
167
Michael Ilseman149209e2012-11-27 00:41:22 +0000168void Instruction::setHasNoNaNs(bool B) {
169 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
170 cast<FPMathOperator>(this)->setHasNoNaNs(B);
171}
172
Michael Ilseman149209e2012-11-27 00:41:22 +0000173void Instruction::setHasNoInfs(bool B) {
174 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
175 cast<FPMathOperator>(this)->setHasNoInfs(B);
176}
177
Michael Ilseman149209e2012-11-27 00:41:22 +0000178void Instruction::setHasNoSignedZeros(bool B) {
179 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
180 cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
181}
182
Michael Ilseman149209e2012-11-27 00:41:22 +0000183void Instruction::setHasAllowReciprocal(bool B) {
184 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
185 cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
186}
187
Sanjay Patel629c4112017-11-06 16:27:15 +0000188void Instruction::setHasApproxFunc(bool B) {
189 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
190 cast<FPMathOperator>(this)->setHasApproxFunc(B);
191}
192
Michael Ilseman149209e2012-11-27 00:41:22 +0000193void Instruction::setFastMathFlags(FastMathFlags FMF) {
194 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
195 cast<FPMathOperator>(this)->setFastMathFlags(FMF);
196}
197
Sanjay Patelb2325b92014-09-02 20:03:00 +0000198void Instruction::copyFastMathFlags(FastMathFlags FMF) {
199 assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
200 cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
201}
202
Sanjay Patel629c4112017-11-06 16:27:15 +0000203bool Instruction::isFast() const {
Chad Rosier829cc2e2014-06-11 18:26:29 +0000204 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
Sanjay Patel629c4112017-11-06 16:27:15 +0000205 return cast<FPMathOperator>(this)->isFast();
206}
207
208bool Instruction::hasAllowReassoc() const {
209 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
210 return cast<FPMathOperator>(this)->hasAllowReassoc();
Michael Ilseman149209e2012-11-27 00:41:22 +0000211}
212
Michael Ilseman149209e2012-11-27 00:41:22 +0000213bool Instruction::hasNoNaNs() const {
Chad Rosier829cc2e2014-06-11 18:26:29 +0000214 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
Michael Ilseman149209e2012-11-27 00:41:22 +0000215 return cast<FPMathOperator>(this)->hasNoNaNs();
216}
217
Michael Ilseman149209e2012-11-27 00:41:22 +0000218bool Instruction::hasNoInfs() const {
Chad Rosier829cc2e2014-06-11 18:26:29 +0000219 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
Michael Ilseman149209e2012-11-27 00:41:22 +0000220 return cast<FPMathOperator>(this)->hasNoInfs();
221}
222
Michael Ilseman149209e2012-11-27 00:41:22 +0000223bool Instruction::hasNoSignedZeros() const {
Chad Rosier829cc2e2014-06-11 18:26:29 +0000224 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
Michael Ilseman149209e2012-11-27 00:41:22 +0000225 return cast<FPMathOperator>(this)->hasNoSignedZeros();
226}
227
Michael Ilseman149209e2012-11-27 00:41:22 +0000228bool Instruction::hasAllowReciprocal() const {
Chad Rosier829cc2e2014-06-11 18:26:29 +0000229 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
Michael Ilseman149209e2012-11-27 00:41:22 +0000230 return cast<FPMathOperator>(this)->hasAllowReciprocal();
231}
232
Adam Nemetcd847a82017-03-28 20:11:52 +0000233bool Instruction::hasAllowContract() const {
234 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
235 return cast<FPMathOperator>(this)->hasAllowContract();
236}
237
Sanjay Patel629c4112017-11-06 16:27:15 +0000238bool Instruction::hasApproxFunc() const {
239 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
240 return cast<FPMathOperator>(this)->hasApproxFunc();
241}
242
Michael Ilseman149209e2012-11-27 00:41:22 +0000243FastMathFlags Instruction::getFastMathFlags() const {
Chad Rosier829cc2e2014-06-11 18:26:29 +0000244 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
Michael Ilseman149209e2012-11-27 00:41:22 +0000245 return cast<FPMathOperator>(this)->getFastMathFlags();
246}
Chris Lattner24a0a432005-08-08 05:21:50 +0000247
Michael Ilseman05d3bf77a2012-11-29 21:25:12 +0000248void Instruction::copyFastMathFlags(const Instruction *I) {
Sanjay Patelb2325b92014-09-02 20:03:00 +0000249 copyFastMathFlags(I->getFastMathFlags());
Michael Ilseman05d3bf77a2012-11-29 21:25:12 +0000250}
251
George Burgess IVa20352e2017-06-09 03:56:15 +0000252void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) {
David Majnemer9554c132016-04-22 06:37:45 +0000253 // Copy the wrapping flags.
George Burgess IVa20352e2017-06-09 03:56:15 +0000254 if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) {
255 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
David Majnemerd0ce8f12016-04-22 06:37:51 +0000256 setHasNoSignedWrap(OB->hasNoSignedWrap());
257 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
258 }
David Majnemer9554c132016-04-22 06:37:45 +0000259 }
260
261 // Copy the exact flag.
262 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
David Majnemerd0ce8f12016-04-22 06:37:51 +0000263 if (isa<PossiblyExactOperator>(this))
264 setIsExact(PE->isExact());
David Majnemer9554c132016-04-22 06:37:45 +0000265
266 // Copy the fast-math flags.
267 if (auto *FP = dyn_cast<FPMathOperator>(V))
David Majnemerd0ce8f12016-04-22 06:37:51 +0000268 if (isa<FPMathOperator>(this))
269 copyFastMathFlags(FP->getFastMathFlags());
David Majnemer92f84cc2016-07-15 05:02:31 +0000270
271 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
272 if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
273 DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds());
David Majnemer9554c132016-04-22 06:37:45 +0000274}
275
276void Instruction::andIRFlags(const Value *V) {
277 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
David Majnemerd0ce8f12016-04-22 06:37:51 +0000278 if (isa<OverflowingBinaryOperator>(this)) {
279 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
280 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
281 }
David Majnemer9554c132016-04-22 06:37:45 +0000282 }
283
284 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
David Majnemerd0ce8f12016-04-22 06:37:51 +0000285 if (isa<PossiblyExactOperator>(this))
286 setIsExact(isExact() & PE->isExact());
David Majnemer9554c132016-04-22 06:37:45 +0000287
288 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
David Majnemerd0ce8f12016-04-22 06:37:51 +0000289 if (isa<FPMathOperator>(this)) {
290 FastMathFlags FM = getFastMathFlags();
291 FM &= FP->getFastMathFlags();
292 copyFastMathFlags(FM);
293 }
David Majnemer9554c132016-04-22 06:37:45 +0000294 }
David Majnemer92f84cc2016-07-15 05:02:31 +0000295
296 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
297 if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
298 DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds());
David Majnemer9554c132016-04-22 06:37:45 +0000299}
Michael Ilseman05d3bf77a2012-11-29 21:25:12 +0000300
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000301const char *Instruction::getOpcodeName(unsigned OpCode) {
302 switch (OpCode) {
303 // Terminators
Chris Lattnerb193ff82002-08-14 18:18:02 +0000304 case Ret: return "ret";
305 case Br: return "br";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000306 case Switch: return "switch";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000307 case IndirectBr: return "indirectbr";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000308 case Invoke: return "invoke";
Bill Wendlingf891bf82011-07-31 06:30:59 +0000309 case Resume: return "resume";
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000310 case Unreachable: return "unreachable";
David Majnemer654e1302015-07-31 17:58:14 +0000311 case CleanupRet: return "cleanupret";
David Majnemer654e1302015-07-31 17:58:14 +0000312 case CatchRet: return "catchret";
313 case CatchPad: return "catchpad";
David Majnemer8a1c45d2015-12-12 05:38:55 +0000314 case CatchSwitch: return "catchswitch";
Craig Topper784929d2019-02-08 20:48:56 +0000315 case CallBr: return "callbr";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000316
Cameron McInallycbde0d92018-11-13 18:15:47 +0000317 // Standard unary operators...
318 case FNeg: return "fneg";
319
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000320 // Standard binary operators...
321 case Add: return "add";
Dan Gohmana5b96452009-06-04 22:49:04 +0000322 case FAdd: return "fadd";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000323 case Sub: return "sub";
Dan Gohmana5b96452009-06-04 22:49:04 +0000324 case FSub: return "fsub";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000325 case Mul: return "mul";
Dan Gohmana5b96452009-06-04 22:49:04 +0000326 case FMul: return "fmul";
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000327 case UDiv: return "udiv";
328 case SDiv: return "sdiv";
329 case FDiv: return "fdiv";
Reid Spencer7eb55b32006-11-02 01:53:59 +0000330 case URem: return "urem";
331 case SRem: return "srem";
332 case FRem: return "frem";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000333
334 // Logical operators...
335 case And: return "and";
336 case Or : return "or";
337 case Xor: return "xor";
338
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000339 // Memory instructions...
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000340 case Alloca: return "alloca";
341 case Load: return "load";
342 case Store: return "store";
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000343 case AtomicCmpXchg: return "cmpxchg";
344 case AtomicRMW: return "atomicrmw";
Eli Friedmanfee02c62011-07-25 23:16:38 +0000345 case Fence: return "fence";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000346 case GetElementPtr: return "getelementptr";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000347
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000348 // Convert instructions...
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000349 case Trunc: return "trunc";
350 case ZExt: return "zext";
351 case SExt: return "sext";
352 case FPTrunc: return "fptrunc";
353 case FPExt: return "fpext";
354 case FPToUI: return "fptoui";
355 case FPToSI: return "fptosi";
356 case UIToFP: return "uitofp";
357 case SIToFP: return "sitofp";
358 case IntToPtr: return "inttoptr";
359 case PtrToInt: return "ptrtoint";
360 case BitCast: return "bitcast";
361 case AddrSpaceCast: return "addrspacecast";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000362
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000363 // Other instructions...
Reid Spencer45e52392006-12-03 06:27:29 +0000364 case ICmp: return "icmp";
365 case FCmp: return "fcmp";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000366 case PHI: return "phi";
367 case Select: return "select";
368 case Call: return "call";
369 case Shl: return "shl";
370 case LShr: return "lshr";
371 case AShr: return "ashr";
372 case VAArg: return "va_arg";
Robert Bocchino23004482006-01-10 19:05:34 +0000373 case ExtractElement: return "extractelement";
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000374 case InsertElement: return "insertelement";
375 case ShuffleVector: return "shufflevector";
Matthijs Kooijmanbf8d6ce2008-05-30 10:31:54 +0000376 case ExtractValue: return "extractvalue";
377 case InsertValue: return "insertvalue";
Bill Wendlingfae14752011-08-12 20:24:12 +0000378 case LandingPad: return "landingpad";
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000379 case CleanupPad: return "cleanuppad";
aqjunee87d7162019-11-07 01:17:49 +0900380 case Freeze: return "freeze";
Chris Lattnerf70da102003-05-08 02:44:12 +0000381
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000382 default: return "<Invalid operator> ";
383 }
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000384}
Chris Lattnercab6c332002-10-31 04:14:01 +0000385
Sanjay Patela40c4792016-10-05 18:51:12 +0000386/// Return true if both instructions have the same special state. This must be
JF Bastien3b6eaac2016-04-11 22:30:37 +0000387/// kept in sync with FunctionComparator::cmpOperations in
388/// lib/Transforms/IPO/MergeFunctions.cpp.
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000389static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
390 bool IgnoreAlignment = false) {
391 assert(I1->getOpcode() == I2->getOpcode() &&
392 "Can not compare special state of different instructions");
393
JF Bastien5502e912016-04-12 18:06:55 +0000394 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
395 return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
396 (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() ||
397 IgnoreAlignment);
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000398 if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
399 return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
400 (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
401 IgnoreAlignment) &&
402 LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000403 LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID();
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000404 if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
405 return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
406 (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
407 IgnoreAlignment) &&
408 SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000409 SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID();
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000410 if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
411 return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
412 if (const CallInst *CI = dyn_cast<CallInst>(I1))
413 return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
414 CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
Sanjoy Das2de4d0a2015-12-14 19:11:35 +0000415 CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
416 CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000417 if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
418 return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
Sanjoy Das2de4d0a2015-12-14 19:11:35 +0000419 CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
420 CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
Craig Topper784929d2019-02-08 20:48:56 +0000421 if (const CallBrInst *CI = dyn_cast<CallBrInst>(I1))
422 return CI->getCallingConv() == cast<CallBrInst>(I2)->getCallingConv() &&
423 CI->getAttributes() == cast<CallBrInst>(I2)->getAttributes() &&
424 CI->hasIdenticalOperandBundleSchema(*cast<CallBrInst>(I2));
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000425 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
426 return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
427 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
428 return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
429 if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
430 return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000431 FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID();
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000432 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
433 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
Tim Northover420a2162014-06-13 14:24:07 +0000434 CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000435 CXI->getSuccessOrdering() ==
436 cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
437 CXI->getFailureOrdering() ==
438 cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000439 CXI->getSyncScopeID() ==
440 cast<AtomicCmpXchgInst>(I2)->getSyncScopeID();
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000441 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
442 return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
443 RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
444 RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000445 RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID();
Eli Friedman1ee6ec22020-03-31 13:08:59 -0700446 if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I1))
447 return SVI->getShuffleMask() ==
448 cast<ShuffleVectorInst>(I2)->getShuffleMask();
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000449
450 return true;
451}
452
Chris Lattner378b0412008-11-27 08:39:18 +0000453bool Instruction::isIdenticalTo(const Instruction *I) const {
Dan Gohman23d2c762009-08-25 22:24:20 +0000454 return isIdenticalToWhenDefined(I) &&
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000455 SubclassOptionalData == I->SubclassOptionalData;
456}
457
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000458bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000459 if (getOpcode() != I->getOpcode() ||
460 getNumOperands() != I->getNumOperands() ||
461 getType() != I->getType())
462 return false;
463
NAKAMURA Takumiaaffd702014-06-02 01:35:34 +0000464 // If both instructions have no operands, they are identical.
465 if (getNumOperands() == 0 && I->getNumOperands() == 0)
466 return haveSameSpecialState(this, I);
467
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000468 // We have two instructions of identical opcode and #operands. Check to see
469 // if all operands are the same.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000470 if (!std::equal(op_begin(), op_end(), I->op_begin()))
471 return false;
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000472
Joel Jones3d90a9a2012-05-10 15:59:41 +0000473 if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
474 const PHINode *otherPHI = cast<PHINode>(I);
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000475 return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
476 otherPHI->block_begin());
Joel Jones3d90a9a2012-05-10 15:59:41 +0000477 }
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000478
479 return haveSameSpecialState(this, I);
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000480}
481
JF Bastien3b6eaac2016-04-11 22:30:37 +0000482// Keep this in sync with FunctionComparator::cmpOperations in
Dan Gohman17fb0d22009-06-12 19:03:05 +0000483// lib/Transforms/IPO/MergeFunctions.cpp.
Hal Finkel74e52252012-06-28 05:42:26 +0000484bool Instruction::isSameOperationAs(const Instruction *I,
485 unsigned flags) const {
486 bool IgnoreAlignment = flags & CompareIgnoringAlignment;
487 bool UseScalarTypes = flags & CompareUsingScalarTypes;
488
Dan Gohman17fb0d22009-06-12 19:03:05 +0000489 if (getOpcode() != I->getOpcode() ||
490 getNumOperands() != I->getNumOperands() ||
Hal Finkel74e52252012-06-28 05:42:26 +0000491 (UseScalarTypes ?
492 getType()->getScalarType() != I->getType()->getScalarType() :
493 getType() != I->getType()))
Reid Spencer266e42b2006-12-23 06:05:41 +0000494 return false;
495
496 // We have two instructions of identical opcode and #operands. Check to see
497 // if all operands are the same type
498 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Hal Finkel74e52252012-06-28 05:42:26 +0000499 if (UseScalarTypes ?
500 getOperand(i)->getType()->getScalarType() !=
501 I->getOperand(i)->getType()->getScalarType() :
502 getOperand(i)->getType() != I->getOperand(i)->getType())
Reid Spencer266e42b2006-12-23 06:05:41 +0000503 return false;
504
Arnaud A. de Grandmaison6a90dc42014-05-27 21:35:46 +0000505 return haveSameSpecialState(this, I, IgnoreAlignment);
Reid Spencer266e42b2006-12-23 06:05:41 +0000506}
507
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000508bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000509 for (const Use &U : uses()) {
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000510 // PHI nodes uses values in the corresponding predecessor block. For other
511 // instructions, just check to see whether the parent of the use matches up.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000512 const Instruction *I = cast<Instruction>(U.getUser());
513 const PHINode *PN = dyn_cast<PHINode>(I);
Craig Topperc6207612014-04-09 06:08:46 +0000514 if (!PN) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000515 if (I->getParent() != BB)
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000516 return true;
517 continue;
518 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000519
Chandler Carruthcdf47882014-03-09 03:16:01 +0000520 if (PN->getIncomingBlock(U) != BB)
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000521 return true;
522 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000523 return false;
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000524}
525
Chris Lattner84627112008-05-08 17:16:51 +0000526bool Instruction::mayReadFromMemory() const {
527 switch (getOpcode()) {
528 default: return false;
Chris Lattner84627112008-05-08 17:16:51 +0000529 case Instruction::VAArg:
Chris Lattner954907a2008-05-08 21:58:49 +0000530 case Instruction::Load:
Eli Friedman89b694b2011-07-27 01:08:30 +0000531 case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
Eli Friedmanadec5872011-07-29 03:05:32 +0000532 case Instruction::AtomicCmpXchg:
533 case Instruction::AtomicRMW:
David Majnemer880c2cb2015-09-10 18:50:09 +0000534 case Instruction::CatchPad:
David Majnemer654e1302015-07-31 17:58:14 +0000535 case Instruction::CatchRet:
Chris Lattner84627112008-05-08 17:16:51 +0000536 return true;
537 case Instruction::Call:
Chris Lattner84627112008-05-08 17:16:51 +0000538 case Instruction::Invoke:
Craig Topper784929d2019-02-08 20:48:56 +0000539 case Instruction::CallBr:
Yevgeny Rouban5e5af532019-10-21 06:52:08 +0000540 return !cast<CallBase>(this)->doesNotReadMemory();
Chris Lattner954907a2008-05-08 21:58:49 +0000541 case Instruction::Store:
Eli Friedmanb9d5a632011-08-15 21:00:18 +0000542 return !cast<StoreInst>(this)->isUnordered();
Chris Lattner84627112008-05-08 17:16:51 +0000543 }
544}
Chris Lattner8ec6e8a2008-04-20 22:11:30 +0000545
Chris Lattnerc992e182007-02-15 23:15:00 +0000546bool Instruction::mayWriteToMemory() const {
547 switch (getOpcode()) {
548 default: return false;
Eli Friedman89b694b2011-07-27 01:08:30 +0000549 case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
Duncan Sands38ef3a82007-12-03 20:06:50 +0000550 case Instruction::Store:
Chris Lattnerc992e182007-02-15 23:15:00 +0000551 case Instruction::VAArg:
Eli Friedmanadec5872011-07-29 03:05:32 +0000552 case Instruction::AtomicCmpXchg:
553 case Instruction::AtomicRMW:
David Majnemer880c2cb2015-09-10 18:50:09 +0000554 case Instruction::CatchPad:
David Majnemer654e1302015-07-31 17:58:14 +0000555 case Instruction::CatchRet:
Chris Lattnerc992e182007-02-15 23:15:00 +0000556 return true;
557 case Instruction::Call:
Chris Lattner84627112008-05-08 17:16:51 +0000558 case Instruction::Invoke:
Craig Topper784929d2019-02-08 20:48:56 +0000559 case Instruction::CallBr:
Craig Topper0b5e6b12019-01-29 23:31:54 +0000560 return !cast<CallBase>(this)->onlyReadsMemory();
Chris Lattnerc992e182007-02-15 23:15:00 +0000561 case Instruction::Load:
Eli Friedmanb9d5a632011-08-15 21:00:18 +0000562 return !cast<LoadInst>(this)->isUnordered();
Chris Lattnerc992e182007-02-15 23:15:00 +0000563 }
564}
Chris Lattnercab6c332002-10-31 04:14:01 +0000565
Robin Morisseted3d48f2014-09-03 21:29:59 +0000566bool Instruction::isAtomic() const {
567 switch (getOpcode()) {
568 default:
569 return false;
570 case Instruction::AtomicCmpXchg:
571 case Instruction::AtomicRMW:
572 case Instruction::Fence:
573 return true;
574 case Instruction::Load:
JF Bastien800f87a2016-04-06 21:19:33 +0000575 return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
Robin Morisseted3d48f2014-09-03 21:29:59 +0000576 case Instruction::Store:
JF Bastien800f87a2016-04-06 21:19:33 +0000577 return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
Robin Morisseted3d48f2014-09-03 21:29:59 +0000578 }
579}
580
Tim Shen04de70d2017-05-09 15:27:17 +0000581bool Instruction::hasAtomicLoad() const {
582 assert(isAtomic());
583 switch (getOpcode()) {
584 default:
585 return false;
586 case Instruction::AtomicCmpXchg:
587 case Instruction::AtomicRMW:
588 case Instruction::Load:
589 return true;
590 }
591}
592
593bool Instruction::hasAtomicStore() const {
594 assert(isAtomic());
595 switch (getOpcode()) {
596 default:
597 return false;
598 case Instruction::AtomicCmpXchg:
599 case Instruction::AtomicRMW:
600 case Instruction::Store:
601 return true;
602 }
603}
604
Duncan Sands1efabaa2009-05-06 06:49:50 +0000605bool Instruction::mayThrow() const {
606 if (const CallInst *CI = dyn_cast<CallInst>(this))
607 return !CI->doesNotThrow();
David Majnemer654e1302015-07-31 17:58:14 +0000608 if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
609 return CRI->unwindsToCaller();
David Majnemer8a1c45d2015-12-12 05:38:55 +0000610 if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
611 return CatchSwitch->unwindsToCaller();
Bill Wendlingd7c6c912011-08-16 21:15:50 +0000612 return isa<ResumeInst>(this);
Duncan Sands1efabaa2009-05-06 06:49:50 +0000613}
614
Chris Bienemanabdea262018-01-09 21:58:46 +0000615bool Instruction::isSafeToRemove() const {
616 return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) &&
Chandler Carruth9ae926b2018-08-26 09:51:22 +0000617 !this->isTerminator();
Chris Bienemanabdea262018-01-09 21:58:46 +0000618}
619
Vedant Kumarb264d692018-12-21 21:49:40 +0000620bool Instruction::isLifetimeStartOrEnd() const {
621 auto II = dyn_cast<IntrinsicInst>(this);
622 if (!II)
623 return false;
624 Intrinsic::ID ID = II->getIntrinsicID();
625 return ID == Intrinsic::lifetime_start || ID == Intrinsic::lifetime_end;
626}
627
Vedant Kumarf01827f2018-06-19 23:42:17 +0000628const Instruction *Instruction::getNextNonDebugInstruction() const {
629 for (const Instruction *I = getNextNode(); I; I = I->getNextNode())
630 if (!isa<DbgInfoIntrinsic>(I))
631 return I;
632 return nullptr;
633}
634
Carlos Alberto Encisofa9cf892018-11-09 09:42:10 +0000635const Instruction *Instruction::getPrevNonDebugInstruction() const {
636 for (const Instruction *I = getPrevNode(); I; I = I->getPrevNode())
637 if (!isa<DbgInfoIntrinsic>(I))
638 return I;
639 return nullptr;
640}
641
Shuxin Yang01ab5d72012-11-29 01:47:31 +0000642bool Instruction::isAssociative() const {
643 unsigned Opcode = getOpcode();
644 if (isAssociative(Opcode))
645 return true;
646
647 switch (Opcode) {
648 case FMul:
649 case FAdd:
Warren Ristowd3efa942018-05-24 20:16:43 +0000650 return cast<FPMathOperator>(this)->hasAllowReassoc() &&
651 cast<FPMathOperator>(this)->hasNoSignedZeros();
Shuxin Yang01ab5d72012-11-29 01:47:31 +0000652 default:
653 return false;
654 }
655}
656
Chandler Carruth96fc1de2018-08-26 08:41:15 +0000657unsigned Instruction::getNumSuccessors() const {
658 switch (getOpcode()) {
659#define HANDLE_TERM_INST(N, OPC, CLASS) \
660 case Instruction::OPC: \
661 return static_cast<const CLASS *>(this)->getNumSuccessors();
662#include "llvm/IR/Instruction.def"
663 default:
664 break;
665 }
666 llvm_unreachable("not a terminator");
667}
668
669BasicBlock *Instruction::getSuccessor(unsigned idx) const {
670 switch (getOpcode()) {
671#define HANDLE_TERM_INST(N, OPC, CLASS) \
672 case Instruction::OPC: \
673 return static_cast<const CLASS *>(this)->getSuccessor(idx);
674#include "llvm/IR/Instruction.def"
675 default:
676 break;
677 }
678 llvm_unreachable("not a terminator");
679}
680
681void Instruction::setSuccessor(unsigned idx, BasicBlock *B) {
682 switch (getOpcode()) {
683#define HANDLE_TERM_INST(N, OPC, CLASS) \
684 case Instruction::OPC: \
685 return static_cast<CLASS *>(this)->setSuccessor(idx, B);
686#include "llvm/IR/Instruction.def"
687 default:
688 break;
689 }
690 llvm_unreachable("not a terminator");
691}
692
Roman Lebedev7ad5d142019-05-05 18:59:22 +0000693void Instruction::replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB) {
694 for (unsigned Idx = 0, NumSuccessors = Instruction::getNumSuccessors();
695 Idx != NumSuccessors; ++Idx)
696 if (getSuccessor(Idx) == OldBB)
697 setSuccessor(Idx, NewBB);
698}
699
Pete Cooper75403d72015-06-24 20:22:23 +0000700Instruction *Instruction::cloneImpl() const {
701 llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
702}
703
Xinliang David Lidc491402016-08-23 15:39:03 +0000704void Instruction::swapProfMetadata() {
705 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
706 if (!ProfileData || ProfileData->getNumOperands() != 3 ||
707 !isa<MDString>(ProfileData->getOperand(0)))
708 return;
709
710 MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
711 if (MDName->getString() != "branch_weights")
712 return;
713
714 // The first operand is the name. Fetch them backwards and build a new one.
715 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
716 ProfileData->getOperand(1)};
717 setMetadata(LLVMContext::MD_prof,
718 MDNode::get(ProfileData->getContext(), Ops));
719}
720
Xinliang David Lidc491402016-08-23 15:39:03 +0000721void Instruction::copyMetadata(const Instruction &SrcInst,
722 ArrayRef<unsigned> WL) {
723 if (!SrcInst.hasMetadata())
724 return;
725
726 DenseSet<unsigned> WLS;
727 for (unsigned M : WL)
728 WLS.insert(M);
729
730 // Otherwise, enumerate and copy over metadata from the old instruction to the
731 // new one.
732 SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
733 SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
734 for (const auto &MD : TheMDs) {
735 if (WL.empty() || WLS.count(MD.first))
736 setMetadata(MD.first, MD.second);
737 }
738 if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
739 setDebugLoc(SrcInst.getDebugLoc());
Xinliang David Lidc491402016-08-23 15:39:03 +0000740}
741
Devang Patel11cf3f42009-10-27 22:16:29 +0000742Instruction *Instruction::clone() const {
Pete Cooper75403d72015-06-24 20:22:23 +0000743 Instruction *New = nullptr;
744 switch (getOpcode()) {
745 default:
746 llvm_unreachable("Unhandled Opcode.");
747#define HANDLE_INST(num, opc, clas) \
748 case Instruction::opc: \
749 New = cast<clas>(this)->cloneImpl(); \
750 break;
751#include "llvm/IR/Instruction.def"
752#undef HANDLE_INST
753 }
754
Devang Patel11cf3f42009-10-27 22:16:29 +0000755 New->SubclassOptionalData = SubclassOptionalData;
Xinliang David Lidc491402016-08-23 15:39:03 +0000756 New->copyMetadata(*this);
Devang Patel11cf3f42009-10-27 22:16:29 +0000757 return New;
758}
Dehao Chene5930492017-03-20 16:40:44 +0000759
Dehao Chen722e9402017-03-23 23:26:00 +0000760void Instruction::setProfWeight(uint64_t W) {
Craig Topper784929d2019-02-08 20:48:56 +0000761 assert(isa<CallBase>(this) &&
762 "Can only set weights for call like instructions");
Dehao Chen722e9402017-03-23 23:26:00 +0000763 SmallVector<uint32_t, 1> Weights;
764 Weights.push_back(W);
765 MDBuilder MDB(getContext());
766 setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
767}