blob: 5a5b9c0d06bbde2ba7232e773e7d3603469bd59f [file] [log] [blame]
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001//===- Instructions.cpp - Implement the LLVM instructions -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000016#include "llvm/ADT/None.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/IR/Attributes.h"
20#include "llvm/IR/BasicBlock.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000021#include "llvm/IR/CallSite.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000022#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Function.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000027#include "llvm/IR/InstrTypes.h"
28#include "llvm/IR/Instruction.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000034#include "llvm/IR/Type.h"
35#include "llvm/IR/Value.h"
36#include "llvm/Support/AtomicOrdering.h"
37#include "llvm/Support/Casting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000039#include "llvm/Support/MathExtras.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000040#include <algorithm>
41#include <cassert>
42#include <cstdint>
43#include <vector>
44
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000045using namespace llvm;
46
Chris Lattner3e13b8c2008-01-02 23:42:30 +000047//===----------------------------------------------------------------------===//
48// CallSite Class
49//===----------------------------------------------------------------------===//
50
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000051User::op_iterator CallSite::getCallee() const {
52 Instruction *II(getInstruction());
53 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000054 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000055 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000056}
57
Gordon Henriksen14a55692007-12-10 02:14:30 +000058//===----------------------------------------------------------------------===//
59// TerminatorInst Class
60//===----------------------------------------------------------------------===//
61
62// Out of line virtual method, so the vtable, etc has a home.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000063TerminatorInst::~TerminatorInst() = default;
Gordon Henriksen14a55692007-12-10 02:14:30 +000064
Reid Kleckner45a13e12017-05-11 21:26:55 +000065unsigned TerminatorInst::getNumSuccessors() const {
66 switch (getOpcode()) {
67#define HANDLE_TERM_INST(N, OPC, CLASS) \
68 case Instruction::OPC: \
69 return static_cast<const CLASS *>(this)->getNumSuccessorsV();
70#include "llvm/IR/Instruction.def"
71 default:
72 break;
73 }
74 llvm_unreachable("not a terminator");
75}
76
77BasicBlock *TerminatorInst::getSuccessor(unsigned idx) const {
78 switch (getOpcode()) {
79#define HANDLE_TERM_INST(N, OPC, CLASS) \
80 case Instruction::OPC: \
81 return static_cast<const CLASS *>(this)->getSuccessorV(idx);
82#include "llvm/IR/Instruction.def"
83 default:
84 break;
85 }
86 llvm_unreachable("not a terminator");
87}
88
89void TerminatorInst::setSuccessor(unsigned idx, BasicBlock *B) {
90 switch (getOpcode()) {
91#define HANDLE_TERM_INST(N, OPC, CLASS) \
92 case Instruction::OPC: \
93 return static_cast<CLASS *>(this)->setSuccessorV(idx, B);
94#include "llvm/IR/Instruction.def"
95 default:
96 break;
97 }
98 llvm_unreachable("not a terminator");
99}
100
Gabor Greiff6caff662008-05-10 08:32:32 +0000101//===----------------------------------------------------------------------===//
102// UnaryInstruction Class
103//===----------------------------------------------------------------------===//
104
Gordon Henriksen14a55692007-12-10 02:14:30 +0000105// Out of line virtual method, so the vtable, etc has a home.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000106UnaryInstruction::~UnaryInstruction() = default;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000107
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000108//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000109// SelectInst Class
110//===----------------------------------------------------------------------===//
111
112/// areInvalidOperands - Return a string if the specified operands are invalid
113/// for a select operation, otherwise return null.
114const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
115 if (Op1->getType() != Op2->getType())
116 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +0000117
118 if (Op1->getType()->isTokenTy())
119 return "select values cannot have token type";
120
Chris Lattner229907c2011-07-18 04:54:35 +0000121 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +0000122 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000123 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000124 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +0000125 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +0000126 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +0000127 return "selected values for vector select must be vectors";
128 if (ET->getNumElements() != VT->getNumElements())
129 return "vector select requires selected vectors to have "
130 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000131 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000132 return "select condition must be i1 or <n x i1>";
133 }
Craig Topperc6207612014-04-09 06:08:46 +0000134 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +0000135}
136
Chris Lattner88107952008-12-29 00:12:50 +0000137//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138// PHINode Class
139//===----------------------------------------------------------------------===//
140
Pete Cooper1d078692015-12-14 20:29:16 +0000141void PHINode::anchor() {}
142
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000143PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000144 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
145 ReservedSpace(PN.getNumOperands()) {
146 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000147 std::copy(PN.op_begin(), PN.op_end(), op_begin());
148 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000149 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000150}
151
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000152// removeIncomingValue - Remove an incoming value. This is useful if a
153// predecessor basic block is deleted.
154Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000155 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156
157 // Move everything after this operand down.
158 //
159 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000160 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000162 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
163 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000164
165 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000166 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000167 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000168
169 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000170 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000171 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000172 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000173 eraseFromParent();
174 }
175 return Removed;
176}
177
Jay Foade98f29d2011-04-01 08:00:58 +0000178/// growOperands - grow operands - This grows the operand list in response
179/// to a push_back style of operation. This grows the number of ops by 1.5
180/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000181///
Jay Foade98f29d2011-04-01 08:00:58 +0000182void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000183 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000184 unsigned NumOps = e + e / 2;
185 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
186
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000187 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000188 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000189}
190
Nate Begemanb3923212005-08-04 23:24:19 +0000191/// hasConstantValue - If the specified PHI node always merges together the same
192/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000193Value *PHINode::hasConstantValue() const {
194 // Exploit the fact that phi nodes always have at least one entry.
195 Value *ConstantValue = getIncomingValue(0);
196 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000197 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
198 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000199 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000200 // The case where the first value is this PHI.
201 ConstantValue = getIncomingValue(i);
202 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000203 if (ConstantValue == this)
204 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000205 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000206}
207
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000208/// hasConstantOrUndefValue - Whether the specified PHI node always merges
209/// together the same value, assuming that undefs result in the same value as
210/// non-undefs.
211/// Unlike \ref hasConstantValue, this does not return a value because the
212/// unique non-undef incoming value need not dominate the PHI node.
213bool PHINode::hasConstantOrUndefValue() const {
214 Value *ConstantValue = nullptr;
215 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
216 Value *Incoming = getIncomingValue(i);
217 if (Incoming != this && !isa<UndefValue>(Incoming)) {
218 if (ConstantValue && ConstantValue != Incoming)
219 return false;
220 ConstantValue = Incoming;
221 }
222 }
223 return true;
224}
225
Bill Wendlingfae14752011-08-12 20:24:12 +0000226//===----------------------------------------------------------------------===//
227// LandingPadInst Implementation
228//===----------------------------------------------------------------------===//
229
David Majnemer7fddecc2015-06-17 20:52:32 +0000230LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
231 const Twine &NameStr, Instruction *InsertBefore)
232 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
233 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000234}
235
David Majnemer7fddecc2015-06-17 20:52:32 +0000236LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
237 const Twine &NameStr, BasicBlock *InsertAtEnd)
238 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
239 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000240}
241
242LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000243 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
244 LP.getNumOperands()),
245 ReservedSpace(LP.getNumOperands()) {
246 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000247 Use *OL = getOperandList();
248 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000249 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
250 OL[I] = InOL[I];
251
252 setCleanup(LP.isCleanup());
253}
254
David Majnemer7fddecc2015-06-17 20:52:32 +0000255LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000256 const Twine &NameStr,
257 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000258 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000259}
260
David Majnemer7fddecc2015-06-17 20:52:32 +0000261LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000262 const Twine &NameStr,
263 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000264 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000265}
266
David Majnemer7fddecc2015-06-17 20:52:32 +0000267void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000268 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000269 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000270 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000271 setName(NameStr);
272 setCleanup(false);
273}
274
275/// growOperands - grow operands - This grows the operand list in response to a
276/// push_back style of operation. This grows the number of ops by 2 times.
277void LandingPadInst::growOperands(unsigned Size) {
278 unsigned e = getNumOperands();
279 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000280 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000281 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000282}
283
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000284void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000285 unsigned OpNo = getNumOperands();
286 growOperands(1);
287 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000288 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000289 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000290}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000291
292//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293// CallInst Implementation
294//===----------------------------------------------------------------------===//
295
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000296CallInst::~CallInst() = default;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000297
David Blaikie348de692015-04-23 21:36:23 +0000298void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000299 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000300 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000301 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
302 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000303 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304
Jay Foad5bd375a2011-07-15 08:37:34 +0000305#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000306 assert((Args.size() == FTy->getNumParams() ||
307 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000308 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000309
310 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000311 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000312 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000313 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000314#endif
315
316 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000317
318 auto It = populateBundleOperandInfos(Bundles, Args.size());
319 (void)It;
320 assert(It + 1 == op_end() && "Should add up!");
321
Jay Foad5bd375a2011-07-15 08:37:34 +0000322 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323}
324
Jay Foad5bd375a2011-07-15 08:37:34 +0000325void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000326 FTy =
327 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000328 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000329 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000330
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000331 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000332
333 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000334}
335
Daniel Dunbar4975db62009-07-25 04:41:11 +0000336CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337 Instruction *InsertBefore)
338 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
339 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000340 Instruction::Call,
341 OperandTraits<CallInst>::op_end(this) - 1,
342 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000343 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344}
345
Daniel Dunbar4975db62009-07-25 04:41:11 +0000346CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347 BasicBlock *InsertAtEnd)
348 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
349 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000350 Instruction::Call,
351 OperandTraits<CallInst>::op_end(this) - 1,
352 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000353 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354}
355
Misha Brukmanb1c93172005-04-21 23:48:37 +0000356CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000357 : Instruction(CI.getType(), Instruction::Call,
358 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
359 CI.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000360 Attrs(CI.Attrs), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000361 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000362 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000363
Jay Foad5bd375a2011-07-15 08:37:34 +0000364 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000365 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
366 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000367 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000368}
369
Sanjoy Das2d161452015-11-18 06:23:38 +0000370CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
371 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000372 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000373
374 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
375 InsertPt);
376 NewCI->setTailCallKind(CI->getTailCallKind());
377 NewCI->setCallingConv(CI->getCallingConv());
378 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000379 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000380 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000381 return NewCI;
382}
383
Hal Finkele87ad542016-07-10 23:01:32 +0000384Value *CallInst::getReturnedArgOperand() const {
385 unsigned Index;
386
Reid Klecknerb5180542017-03-21 16:57:19 +0000387 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000388 return getArgOperand(Index - AttributeList::FirstArgIndex);
Hal Finkele87ad542016-07-10 23:01:32 +0000389 if (const Function *F = getCalledFunction())
390 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
391 Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000392 return getArgOperand(Index - AttributeList::FirstArgIndex);
393
Hal Finkele87ad542016-07-10 23:01:32 +0000394 return nullptr;
395}
396
Amaury Sechet392638d2016-06-14 20:27:35 +0000397void CallInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000398 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000399 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000400 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000401}
402
Amaury Secheta65a2372016-06-15 05:14:29 +0000403void CallInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000404 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000405 PAL = PAL.addAttribute(getContext(), i, Attr);
406 setAttributes(PAL);
407}
408
Reid Klecknera0b45f42017-05-03 18:17:31 +0000409void CallInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
410 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
411}
412
Amaury Sechet392638d2016-06-14 20:27:35 +0000413void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000414 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000415 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000416 setAttributes(PAL);
417}
418
Amaury Sechet6100adf2016-06-15 17:50:39 +0000419void CallInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000420 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000421 PAL = PAL.removeAttribute(getContext(), i, Kind);
422 setAttributes(PAL);
423}
424
Reid Klecknera0b45f42017-05-03 18:17:31 +0000425void CallInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
426 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
427}
428
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000429void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000430 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000431 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
432 setAttributes(PAL);
433}
434
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000435void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000436 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000437 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
438 setAttributes(PAL);
439}
440
Reid Klecknerfb502d22017-04-14 20:19:02 +0000441bool CallInst::hasRetAttr(Attribute::AttrKind Kind) const {
442 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
443 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000444
Reid Klecknerfb502d22017-04-14 20:19:02 +0000445 // Look at the callee, if available.
446 if (const Function *F = getCalledFunction())
447 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
448 return false;
449}
450
451bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
452 assert(i < getNumArgOperands() && "Param index out of bounds!");
453
454 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000455 return true;
456 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000457 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000458 return false;
459}
460
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000461bool CallInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000462 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000463 // There are getNumOperands() - 1 data operands. The last operand is the
464 // callee.
465 assert(i < getNumOperands() && "Data operand index out of bounds!");
466
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000467 // The attribute A can either be directly specified, if the operand in
468 // question is a call argument; or be indirectly implied by the kind of its
469 // containing operand bundle, if the operand is a bundle operand.
470
Reid Klecknerfb502d22017-04-14 20:19:02 +0000471 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
472 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000473 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000474 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000475
476 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
477 "Must be either a call argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000478 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000479}
480
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000481/// IsConstantOne - Return true only if val is constant int 1
482static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000483 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000484 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
485 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000486}
487
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000488static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000489 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000490 Type *AllocTy, Value *AllocSize,
491 Value *ArraySize,
492 ArrayRef<OperandBundleDef> OpB,
493 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000494 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000495 "createMalloc needs either InsertBefore or InsertAtEnd");
496
497 // malloc(type) becomes:
498 // bitcast (i8* malloc(typeSize)) to type*
499 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000500 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000501 if (!ArraySize)
502 ArraySize = ConstantInt::get(IntPtrTy, 1);
503 else if (ArraySize->getType() != IntPtrTy) {
504 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000505 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
506 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000507 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000508 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
509 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000510 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000511
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000512 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000513 if (IsConstantOne(AllocSize)) {
514 AllocSize = ArraySize; // Operand * 1 = Operand
515 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
516 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
517 false /*ZExt*/);
518 // Malloc arg is constant product of type size and array size
519 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
520 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000521 // Multiply type size by the array size...
522 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000523 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
524 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000525 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000526 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
527 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000528 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000529 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000530
Victor Hernandez788eaab2009-09-18 19:20:02 +0000531 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000532 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000533 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
534 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000535 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000536 Value *MallocFunc = MallocF;
537 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000538 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000539 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000540 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000541 CallInst *MCall = nullptr;
542 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000543 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000544 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
545 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000546 Result = MCall;
547 if (Result->getType() != AllocPtrType)
548 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000549 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000550 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000551 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000552 Result = MCall;
553 if (Result->getType() != AllocPtrType) {
554 InsertAtEnd->getInstList().push_back(MCall);
555 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000556 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000557 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000558 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000559 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000560 if (Function *F = dyn_cast<Function>(MallocFunc)) {
561 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000562 if (!F->returnDoesNotAlias())
563 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000564 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000565 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000566
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000567 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000568}
569
570/// CreateMalloc - Generate the IR for a call to malloc:
571/// 1. Compute the malloc call's argument as the specified type's size,
572/// possibly multiplied by the array size if the array size is not
573/// constant 1.
574/// 2. Call malloc with that argument.
575/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000576Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000577 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000578 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000579 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000580 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000581 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000582 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000583}
David Majnemerfadc6db2016-04-29 08:07:22 +0000584Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
585 Type *IntPtrTy, Type *AllocTy,
586 Value *AllocSize, Value *ArraySize,
587 ArrayRef<OperandBundleDef> OpB,
588 Function *MallocF,
589 const Twine &Name) {
590 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
591 ArraySize, OpB, MallocF, Name);
592}
593
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000594/// CreateMalloc - Generate the IR for a call to malloc:
595/// 1. Compute the malloc call's argument as the specified type's size,
596/// possibly multiplied by the array size if the array size is not
597/// constant 1.
598/// 2. Call malloc with that argument.
599/// 3. Bitcast the result of the malloc call to the specified type.
600/// Note: This function does not add the bitcast to the basic block, that is the
601/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000602Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000603 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000604 Value *AllocSize, Value *ArraySize,
605 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000606 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000607 ArraySize, None, MallocF, Name);
608}
609Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
610 Type *IntPtrTy, Type *AllocTy,
611 Value *AllocSize, Value *ArraySize,
612 ArrayRef<OperandBundleDef> OpB,
613 Function *MallocF, const Twine &Name) {
614 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
615 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000616}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000617
David Majnemerfadc6db2016-04-29 08:07:22 +0000618static Instruction *createFree(Value *Source,
619 ArrayRef<OperandBundleDef> Bundles,
620 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000621 BasicBlock *InsertAtEnd) {
622 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
623 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000624 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000625 "Can not free something of nonpointer type!");
626
Ana Pazosb3596022016-02-03 21:34:39 +0000627 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
628 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000629
Chris Lattner229907c2011-07-18 04:54:35 +0000630 Type *VoidTy = Type::getVoidTy(M->getContext());
631 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000632 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000633 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000634 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000635 Value *PtrCast = Source;
636 if (InsertBefore) {
637 if (Source->getType() != IntPtrTy)
638 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000639 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000640 } else {
641 if (Source->getType() != IntPtrTy)
642 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000643 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000644 }
645 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000646 if (Function *F = dyn_cast<Function>(FreeFunc))
647 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000648
649 return Result;
650}
651
652/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000653Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000654 return createFree(Source, None, InsertBefore, nullptr);
655}
656Instruction *CallInst::CreateFree(Value *Source,
657 ArrayRef<OperandBundleDef> Bundles,
658 Instruction *InsertBefore) {
659 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000660}
661
662/// CreateFree - Generate the IR for a call to the builtin free function.
663/// Note: This function does not add the call to the basic block, that is the
664/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000665Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000666 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
667 assert(FreeCall && "CreateFree did not create a CallInst");
668 return FreeCall;
669}
670Instruction *CallInst::CreateFree(Value *Source,
671 ArrayRef<OperandBundleDef> Bundles,
672 BasicBlock *InsertAtEnd) {
673 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000674 assert(FreeCall && "CreateFree did not create a CallInst");
675 return FreeCall;
676}
677
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000678//===----------------------------------------------------------------------===//
679// InvokeInst Implementation
680//===----------------------------------------------------------------------===//
681
David Blaikie3e807092015-05-13 18:35:26 +0000682void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
683 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000684 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000685 const Twine &NameStr) {
686 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000687
Sanjoy Das9303c242015-09-24 19:14:18 +0000688 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
689 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000690 Op<-3>() = Fn;
691 Op<-2>() = IfNormal;
692 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000693
694#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000695 assert(((Args.size() == FTy->getNumParams()) ||
696 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000697 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000698
Jay Foad5bd375a2011-07-15 08:37:34 +0000699 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000700 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000701 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000702 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000703#endif
704
705 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000706
707 auto It = populateBundleOperandInfos(Bundles, Args.size());
708 (void)It;
709 assert(It + 3 == op_end() && "Should add up!");
710
Jay Foad5bd375a2011-07-15 08:37:34 +0000711 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000712}
713
Misha Brukmanb1c93172005-04-21 23:48:37 +0000714InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000715 : TerminatorInst(II.getType(), Instruction::Invoke,
716 OperandTraits<InvokeInst>::op_end(this) -
717 II.getNumOperands(),
718 II.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000719 Attrs(II.Attrs), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000720 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000721 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000722 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
723 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000724 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Sanjoy Das2d161452015-11-18 06:23:38 +0000727InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
728 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000729 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000730
731 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
732 II->getUnwindDest(), Args, OpB,
733 II->getName(), InsertPt);
734 NewII->setCallingConv(II->getCallingConv());
735 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000736 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000737 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000738 return NewII;
739}
740
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000741BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
742 return getSuccessor(idx);
743}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000744
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000745unsigned InvokeInst::getNumSuccessorsV() const {
746 return getNumSuccessors();
747}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000748
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000749void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
750 return setSuccessor(idx, B);
751}
752
Hal Finkele87ad542016-07-10 23:01:32 +0000753Value *InvokeInst::getReturnedArgOperand() const {
754 unsigned Index;
755
Reid Klecknerb5180542017-03-21 16:57:19 +0000756 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000757 return getArgOperand(Index - AttributeList::FirstArgIndex);
Hal Finkele87ad542016-07-10 23:01:32 +0000758 if (const Function *F = getCalledFunction())
759 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
760 Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000761 return getArgOperand(Index - AttributeList::FirstArgIndex);
762
Hal Finkele87ad542016-07-10 23:01:32 +0000763 return nullptr;
764}
765
Reid Klecknerfb502d22017-04-14 20:19:02 +0000766bool InvokeInst::hasRetAttr(Attribute::AttrKind Kind) const {
767 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
768 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000769
Reid Klecknerfb502d22017-04-14 20:19:02 +0000770 // Look at the callee, if available.
771 if (const Function *F = getCalledFunction())
772 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
773 return false;
774}
775
776bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
777 assert(i < getNumArgOperands() && "Param index out of bounds!");
778
779 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000780 return true;
781 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000782 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000783 return false;
784}
785
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000786bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000787 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000788 // There are getNumOperands() - 3 data operands. The last three operands are
789 // the callee and the two successor basic blocks.
790 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
791
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000792 // The attribute A can either be directly specified, if the operand in
793 // question is an invoke argument; or be indirectly implied by the kind of its
794 // containing operand bundle, if the operand is a bundle operand.
795
Reid Klecknerfb502d22017-04-14 20:19:02 +0000796 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
797 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000798 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000799 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000800
801 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
802 "Must be either an invoke argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000803 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000804}
805
Amaury Sechet392638d2016-06-14 20:27:35 +0000806void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000807 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000808 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000809 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000810}
811
Amaury Secheta65a2372016-06-15 05:14:29 +0000812void InvokeInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000813 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000814 PAL = PAL.addAttribute(getContext(), i, Attr);
815 setAttributes(PAL);
816}
817
Reid Klecknera0b45f42017-05-03 18:17:31 +0000818void InvokeInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
819 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
820}
821
Amaury Sechet392638d2016-06-14 20:27:35 +0000822void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000823 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000824 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000825 setAttributes(PAL);
826}
827
Amaury Sechet6100adf2016-06-15 17:50:39 +0000828void InvokeInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000829 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000830 PAL = PAL.removeAttribute(getContext(), i, Kind);
831 setAttributes(PAL);
832}
833
Reid Klecknera0b45f42017-05-03 18:17:31 +0000834void InvokeInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
835 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
836}
837
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000838void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000839 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000840 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
841 setAttributes(PAL);
842}
843
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000844void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000845 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000846 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
847 setAttributes(PAL);
848}
849
Bill Wendlingfae14752011-08-12 20:24:12 +0000850LandingPadInst *InvokeInst::getLandingPadInst() const {
851 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
852}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000853
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000854//===----------------------------------------------------------------------===//
855// ReturnInst Implementation
856//===----------------------------------------------------------------------===//
857
Chris Lattner2195fc42007-02-24 00:55:48 +0000858ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000859 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000860 OperandTraits<ReturnInst>::op_end(this) -
861 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000862 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000863 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000864 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000865 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000866}
867
Owen Anderson55f1c092009-08-13 21:58:54 +0000868ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
869 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000870 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
871 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000872 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000873 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000874}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000875
Owen Anderson55f1c092009-08-13 21:58:54 +0000876ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
877 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000878 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
879 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000880 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000881 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000882}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000883
Owen Anderson55f1c092009-08-13 21:58:54 +0000884ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
885 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000886 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000887}
888
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000889unsigned ReturnInst::getNumSuccessorsV() const {
890 return getNumSuccessors();
891}
892
Devang Patelae682fb2008-02-26 17:56:20 +0000893/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
894/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000895void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000896 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000897}
898
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000899BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000900 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000901}
902
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000903ReturnInst::~ReturnInst() = default;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000904
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000906// ResumeInst Implementation
907//===----------------------------------------------------------------------===//
908
909ResumeInst::ResumeInst(const ResumeInst &RI)
910 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
911 OperandTraits<ResumeInst>::op_begin(this), 1) {
912 Op<0>() = RI.Op<0>();
913}
914
915ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
916 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
917 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
918 Op<0>() = Exn;
919}
920
921ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
922 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
923 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
924 Op<0>() = Exn;
925}
926
927unsigned ResumeInst::getNumSuccessorsV() const {
928 return getNumSuccessors();
929}
930
931void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
932 llvm_unreachable("ResumeInst has no successors!");
933}
934
935BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
936 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000937}
938
939//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000940// CleanupReturnInst Implementation
941//===----------------------------------------------------------------------===//
942
943CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
944 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
945 OperandTraits<CleanupReturnInst>::op_end(this) -
946 CRI.getNumOperands(),
947 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000948 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000949 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000950 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000951 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000952}
953
David Majnemer8a1c45d2015-12-12 05:38:55 +0000954void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000955 if (UnwindBB)
956 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000957
David Majnemer8a1c45d2015-12-12 05:38:55 +0000958 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000959 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000960 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000961}
962
David Majnemer8a1c45d2015-12-12 05:38:55 +0000963CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
964 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000965 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
966 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000967 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
968 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000969 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000970}
971
David Majnemer8a1c45d2015-12-12 05:38:55 +0000972CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
973 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000974 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
975 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000976 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
977 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000978 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000979}
980
981BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
982 assert(Idx == 0);
983 return getUnwindDest();
984}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000985
David Majnemer654e1302015-07-31 17:58:14 +0000986unsigned CleanupReturnInst::getNumSuccessorsV() const {
987 return getNumSuccessors();
988}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000989
David Majnemer654e1302015-07-31 17:58:14 +0000990void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
991 assert(Idx == 0);
992 setUnwindDest(B);
993}
994
995//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000996// CatchReturnInst Implementation
997//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000998void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000999 Op<0>() = CatchPad;
1000 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +00001001}
David Majnemer654e1302015-07-31 17:58:14 +00001002
1003CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
1004 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001005 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
1006 Op<0>() = CRI.Op<0>();
1007 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +00001008}
1009
David Majnemer8a1c45d2015-12-12 05:38:55 +00001010CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +00001011 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +00001012 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001013 OperandTraits<CatchReturnInst>::op_begin(this), 2,
1014 InsertBefore) {
1015 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +00001016}
1017
David Majnemer8a1c45d2015-12-12 05:38:55 +00001018CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +00001019 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +00001020 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001021 OperandTraits<CatchReturnInst>::op_begin(this), 2,
1022 InsertAtEnd) {
1023 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +00001024}
1025
1026BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +00001027 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +00001028 return getSuccessor();
1029}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001030
David Majnemer654e1302015-07-31 17:58:14 +00001031unsigned CatchReturnInst::getNumSuccessorsV() const {
1032 return getNumSuccessors();
1033}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001034
David Majnemer654e1302015-07-31 17:58:14 +00001035void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +00001036 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +00001037 setSuccessor(B);
1038}
1039
1040//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +00001041// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +00001042//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +00001043
1044CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
1045 unsigned NumReservedValues,
1046 const Twine &NameStr,
1047 Instruction *InsertBefore)
1048 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
1049 InsertBefore) {
1050 if (UnwindDest)
1051 ++NumReservedValues;
1052 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +00001053 setName(NameStr);
1054}
1055
David Majnemer8a1c45d2015-12-12 05:38:55 +00001056CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
1057 unsigned NumReservedValues,
1058 const Twine &NameStr, BasicBlock *InsertAtEnd)
1059 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +00001060 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00001061 if (UnwindDest)
1062 ++NumReservedValues;
1063 init(ParentPad, UnwindDest, NumReservedValues + 1);
1064 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001065}
1066
David Majnemer8a1c45d2015-12-12 05:38:55 +00001067CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
1068 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
1069 CSI.getNumOperands()) {
1070 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
1071 setNumHungOffUseOperands(ReservedSpace);
1072 Use *OL = getOperandList();
1073 const Use *InOL = CSI.getOperandList();
1074 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
1075 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +00001076}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001077
1078void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
1079 unsigned NumReservedValues) {
1080 assert(ParentPad && NumReservedValues);
1081
1082 ReservedSpace = NumReservedValues;
1083 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
1084 allocHungoffUses(ReservedSpace);
1085
1086 Op<0>() = ParentPad;
1087 if (UnwindDest) {
1088 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1089 setUnwindDest(UnwindDest);
1090 }
1091}
1092
1093/// growOperands - grow operands - This grows the operand list in response to a
1094/// push_back style of operation. This grows the number of ops by 2 times.
1095void CatchSwitchInst::growOperands(unsigned Size) {
1096 unsigned NumOperands = getNumOperands();
1097 assert(NumOperands >= 1);
1098 if (ReservedSpace >= NumOperands + Size)
1099 return;
1100 ReservedSpace = (NumOperands + Size / 2) * 2;
1101 growHungoffUses(ReservedSpace);
1102}
1103
1104void CatchSwitchInst::addHandler(BasicBlock *Handler) {
1105 unsigned OpNo = getNumOperands();
1106 growOperands(1);
1107 assert(OpNo < ReservedSpace && "Growing didn't work!");
1108 setNumHungOffUseOperands(getNumOperands() + 1);
1109 getOperandList()[OpNo] = Handler;
1110}
1111
Joseph Tremoulet0d808882016-01-05 02:37:41 +00001112void CatchSwitchInst::removeHandler(handler_iterator HI) {
1113 // Move all subsequent handlers up one.
1114 Use *EndDst = op_end() - 1;
1115 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
1116 *CurDst = *(CurDst + 1);
1117 // Null out the last handler use.
1118 *EndDst = nullptr;
1119
1120 setNumHungOffUseOperands(getNumOperands() - 1);
1121}
1122
David Majnemer8a1c45d2015-12-12 05:38:55 +00001123BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
1124 return getSuccessor(idx);
1125}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001126
David Majnemer8a1c45d2015-12-12 05:38:55 +00001127unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +00001128 return getNumSuccessors();
1129}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001130
David Majnemer8a1c45d2015-12-12 05:38:55 +00001131void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1132 setSuccessor(idx, B);
1133}
1134
1135//===----------------------------------------------------------------------===//
1136// FuncletPadInst Implementation
1137//===----------------------------------------------------------------------===//
1138void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1139 const Twine &NameStr) {
1140 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
1141 std::copy(Args.begin(), Args.end(), op_begin());
1142 setParentPad(ParentPad);
1143 setName(NameStr);
1144}
1145
1146FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1147 : Instruction(FPI.getType(), FPI.getOpcode(),
1148 OperandTraits<FuncletPadInst>::op_end(this) -
1149 FPI.getNumOperands(),
1150 FPI.getNumOperands()) {
1151 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1152 setParentPad(FPI.getParentPad());
1153}
1154
1155FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1156 ArrayRef<Value *> Args, unsigned Values,
1157 const Twine &NameStr, Instruction *InsertBefore)
1158 : Instruction(ParentPad->getType(), Op,
1159 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1160 InsertBefore) {
1161 init(ParentPad, Args, NameStr);
1162}
1163
1164FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1165 ArrayRef<Value *> Args, unsigned Values,
1166 const Twine &NameStr, BasicBlock *InsertAtEnd)
1167 : Instruction(ParentPad->getType(), Op,
1168 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1169 InsertAtEnd) {
1170 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001171}
1172
1173//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001174// UnreachableInst Implementation
1175//===----------------------------------------------------------------------===//
1176
Owen Anderson55f1c092009-08-13 21:58:54 +00001177UnreachableInst::UnreachableInst(LLVMContext &Context,
1178 Instruction *InsertBefore)
1179 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001180 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001181}
Owen Anderson55f1c092009-08-13 21:58:54 +00001182UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1183 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001184 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001185}
1186
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001187unsigned UnreachableInst::getNumSuccessorsV() const {
1188 return getNumSuccessors();
1189}
1190
1191void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001192 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001193}
1194
1195BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001196 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001197}
1198
1199//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001200// BranchInst Implementation
1201//===----------------------------------------------------------------------===//
1202
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001203void BranchInst::AssertOK() {
1204 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001205 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001206 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001207}
1208
Chris Lattner2195fc42007-02-24 00:55:48 +00001209BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001210 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001211 OperandTraits<BranchInst>::op_end(this) - 1,
1212 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001213 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001214 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001215}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001216
Chris Lattner2195fc42007-02-24 00:55:48 +00001217BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1218 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001219 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001220 OperandTraits<BranchInst>::op_end(this) - 3,
1221 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001222 Op<-1>() = IfTrue;
1223 Op<-2>() = IfFalse;
1224 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001225#ifndef NDEBUG
1226 AssertOK();
1227#endif
1228}
1229
1230BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001231 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001232 OperandTraits<BranchInst>::op_end(this) - 1,
1233 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001234 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001235 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001236}
1237
1238BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1239 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001240 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001241 OperandTraits<BranchInst>::op_end(this) - 3,
1242 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001243 Op<-1>() = IfTrue;
1244 Op<-2>() = IfFalse;
1245 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001246#ifndef NDEBUG
1247 AssertOK();
1248#endif
1249}
1250
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001251BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001252 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001253 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1254 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001255 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001256 if (BI.getNumOperands() != 1) {
1257 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001258 Op<-3>() = BI.Op<-3>();
1259 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001260 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001261 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001262}
1263
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001264void BranchInst::swapSuccessors() {
1265 assert(isConditional() &&
1266 "Cannot swap successors of an unconditional branch");
1267 Op<-1>().swap(Op<-2>());
1268
1269 // Update profile metadata if present and it matches our structural
1270 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001271 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001272}
1273
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001274BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1275 return getSuccessor(idx);
1276}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001277
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001278unsigned BranchInst::getNumSuccessorsV() const {
1279 return getNumSuccessors();
1280}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001281
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001282void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1283 setSuccessor(idx, B);
1284}
1285
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001286//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001287// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001288//===----------------------------------------------------------------------===//
1289
Owen Andersonb6b25302009-07-14 23:09:55 +00001290static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001291 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001292 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001293 else {
1294 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001295 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001296 assert(Amt->getType()->isIntegerTy() &&
1297 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001298 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001299 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001300}
1301
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001302AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001303 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001304 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001305
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001306AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001307 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001308 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001309
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001310AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001311 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001312 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1313
1314AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1315 const Twine &Name, BasicBlock *InsertAtEnd)
1316 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1317
1318AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1319 unsigned Align, const Twine &Name,
1320 Instruction *InsertBefore)
1321 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1322 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1323 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001324 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001325 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001326 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001327}
1328
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001329AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1330 unsigned Align, const Twine &Name,
1331 BasicBlock *InsertAtEnd)
1332 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1333 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001334 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001335 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001336 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001337 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001338}
1339
Gordon Henriksen14a55692007-12-10 02:14:30 +00001340// Out of line virtual method, so the vtable, etc has a home.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001341AllocaInst::~AllocaInst() = default;
Gordon Henriksen14a55692007-12-10 02:14:30 +00001342
Victor Hernandez8acf2952009-10-23 21:09:37 +00001343void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001344 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001345 assert(Align <= MaximumAlignment &&
1346 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001347 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1348 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001349 assert(getAlignment() == Align && "Alignment representation error!");
1350}
1351
Victor Hernandez8acf2952009-10-23 21:09:37 +00001352bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001353 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001354 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001355 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001356}
1357
Chris Lattner8b291e62008-11-26 02:54:17 +00001358/// isStaticAlloca - Return true if this alloca is in the entry block of the
1359/// function and is a constant size. If so, the code generator will fold it
1360/// into the prolog/epilog code, so it is basically free.
1361bool AllocaInst::isStaticAlloca() const {
1362 // Must be constant size.
1363 if (!isa<ConstantInt>(getArraySize())) return false;
1364
1365 // Must be in the entry block.
1366 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001367 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001368}
1369
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001370//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001371// LoadInst Implementation
1372//===----------------------------------------------------------------------===//
1373
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001374void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001375 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001376 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001377 assert(!(isAtomic() && getAlignment() == 0) &&
1378 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001379}
1380
Daniel Dunbar4975db62009-07-25 04:41:11 +00001381LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001382 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001383
Daniel Dunbar4975db62009-07-25 04:41:11 +00001384LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001385 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001386
David Blaikie0c28fd72015-05-20 21:46:30 +00001387LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001388 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001389 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001390
1391LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1392 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001393 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001394
David Blaikieb7a029872015-04-17 19:56:21 +00001395LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001396 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001397 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1398 CrossThread, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001399
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001400LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001401 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001402 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1403 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001404
David Blaikie15d9a4c2015-04-06 20:59:48 +00001405LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001406 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001407 SynchronizationScope SynchScope, Instruction *InsertBef)
1408 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001409 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001410 setVolatile(isVolatile);
1411 setAlignment(Align);
1412 setAtomic(Order, SynchScope);
1413 AssertOK();
1414 setName(Name);
1415}
1416
1417LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1418 unsigned Align, AtomicOrdering Order,
1419 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001420 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001421 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001422 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001423 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001424 setAlignment(Align);
1425 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001426 AssertOK();
1427 setName(Name);
1428}
1429
Daniel Dunbar27096822009-08-11 18:11:15 +00001430LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1431 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1432 Load, Ptr, InsertBef) {
1433 setVolatile(false);
1434 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001435 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001436 AssertOK();
1437 if (Name && Name[0]) setName(Name);
1438}
1439
1440LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1441 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1442 Load, Ptr, InsertAE) {
1443 setVolatile(false);
1444 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001445 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001446 AssertOK();
1447 if (Name && Name[0]) setName(Name);
1448}
1449
David Blaikie0c28fd72015-05-20 21:46:30 +00001450LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001451 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001452 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1453 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001454 setVolatile(isVolatile);
1455 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001456 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001457 AssertOK();
1458 if (Name && Name[0]) setName(Name);
1459}
1460
1461LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1462 BasicBlock *InsertAE)
1463 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1464 Load, Ptr, InsertAE) {
1465 setVolatile(isVolatile);
1466 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001467 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001468 AssertOK();
1469 if (Name && Name[0]) setName(Name);
1470}
1471
Christopher Lamb84485702007-04-22 19:24:39 +00001472void LoadInst::setAlignment(unsigned Align) {
1473 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001474 assert(Align <= MaximumAlignment &&
1475 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001476 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001477 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001478 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001479}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001480
1481//===----------------------------------------------------------------------===//
1482// StoreInst Implementation
1483//===----------------------------------------------------------------------===//
1484
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001485void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001486 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001487 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001488 "Ptr must have pointer type!");
1489 assert(getOperand(0)->getType() ==
1490 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001491 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001492 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001493 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001494}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001495
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001496StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001497 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001498
1499StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001500 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001501
Misha Brukmanb1c93172005-04-21 23:48:37 +00001502StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001503 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001504 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001505
1506StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001507 BasicBlock *InsertAtEnd)
1508 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1509
1510StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1511 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001512 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1513 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001514
1515StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1516 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001517 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1518 CrossThread, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001519
Misha Brukmanb1c93172005-04-21 23:48:37 +00001520StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001521 unsigned Align, AtomicOrdering Order,
1522 SynchronizationScope SynchScope,
1523 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001524 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001525 OperandTraits<StoreInst>::op_begin(this),
1526 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001527 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001528 Op<0>() = val;
1529 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001530 setVolatile(isVolatile);
1531 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001532 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001533 AssertOK();
1534}
1535
1536StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001537 unsigned Align, AtomicOrdering Order,
1538 SynchronizationScope SynchScope,
1539 BasicBlock *InsertAtEnd)
1540 : Instruction(Type::getVoidTy(val->getContext()), Store,
1541 OperandTraits<StoreInst>::op_begin(this),
1542 OperandTraits<StoreInst>::operands(this),
1543 InsertAtEnd) {
1544 Op<0>() = val;
1545 Op<1>() = addr;
1546 setVolatile(isVolatile);
1547 setAlignment(Align);
1548 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001549 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001550}
1551
Christopher Lamb84485702007-04-22 19:24:39 +00001552void StoreInst::setAlignment(unsigned Align) {
1553 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001554 assert(Align <= MaximumAlignment &&
1555 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001556 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001557 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001558 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001559}
1560
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001561//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001562// AtomicCmpXchgInst Implementation
1563//===----------------------------------------------------------------------===//
1564
1565void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001566 AtomicOrdering SuccessOrdering,
1567 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001568 SynchronizationScope SynchScope) {
1569 Op<0>() = Ptr;
1570 Op<1>() = Cmp;
1571 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001572 setSuccessOrdering(SuccessOrdering);
1573 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001574 setSynchScope(SynchScope);
1575
1576 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1577 "All operands must be non-null!");
1578 assert(getOperand(0)->getType()->isPointerTy() &&
1579 "Ptr must have pointer type!");
1580 assert(getOperand(1)->getType() ==
1581 cast<PointerType>(getOperand(0)->getType())->getElementType()
1582 && "Ptr must be a pointer to Cmp type!");
1583 assert(getOperand(2)->getType() ==
1584 cast<PointerType>(getOperand(0)->getType())->getElementType()
1585 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001586 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001587 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001588 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001589 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001590 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1591 "AtomicCmpXchg failure argument shall be no stronger than the success "
1592 "argument");
1593 assert(FailureOrdering != AtomicOrdering::Release &&
1594 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001595 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001596}
1597
1598AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001599 AtomicOrdering SuccessOrdering,
1600 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001601 SynchronizationScope SynchScope,
1602 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001603 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001604 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001605 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1606 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001607 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001608}
1609
1610AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001611 AtomicOrdering SuccessOrdering,
1612 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001613 SynchronizationScope SynchScope,
1614 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001615 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001616 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001617 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1618 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001619 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001620}
Tim Northover420a2162014-06-13 14:24:07 +00001621
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001622//===----------------------------------------------------------------------===//
1623// AtomicRMWInst Implementation
1624//===----------------------------------------------------------------------===//
1625
1626void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1627 AtomicOrdering Ordering,
1628 SynchronizationScope SynchScope) {
1629 Op<0>() = Ptr;
1630 Op<1>() = Val;
1631 setOperation(Operation);
1632 setOrdering(Ordering);
1633 setSynchScope(SynchScope);
1634
1635 assert(getOperand(0) && getOperand(1) &&
1636 "All operands must be non-null!");
1637 assert(getOperand(0)->getType()->isPointerTy() &&
1638 "Ptr must have pointer type!");
1639 assert(getOperand(1)->getType() ==
1640 cast<PointerType>(getOperand(0)->getType())->getElementType()
1641 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001642 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001643 "AtomicRMW instructions must be atomic!");
1644}
1645
1646AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1647 AtomicOrdering Ordering,
1648 SynchronizationScope SynchScope,
1649 Instruction *InsertBefore)
1650 : Instruction(Val->getType(), AtomicRMW,
1651 OperandTraits<AtomicRMWInst>::op_begin(this),
1652 OperandTraits<AtomicRMWInst>::operands(this),
1653 InsertBefore) {
1654 Init(Operation, Ptr, Val, Ordering, SynchScope);
1655}
1656
1657AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1658 AtomicOrdering Ordering,
1659 SynchronizationScope SynchScope,
1660 BasicBlock *InsertAtEnd)
1661 : Instruction(Val->getType(), AtomicRMW,
1662 OperandTraits<AtomicRMWInst>::op_begin(this),
1663 OperandTraits<AtomicRMWInst>::operands(this),
1664 InsertAtEnd) {
1665 Init(Operation, Ptr, Val, Ordering, SynchScope);
1666}
1667
1668//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001669// FenceInst Implementation
1670//===----------------------------------------------------------------------===//
1671
1672FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1673 SynchronizationScope SynchScope,
1674 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001675 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001676 setOrdering(Ordering);
1677 setSynchScope(SynchScope);
1678}
1679
1680FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1681 SynchronizationScope SynchScope,
1682 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001683 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001684 setOrdering(Ordering);
1685 setSynchScope(SynchScope);
1686}
1687
1688//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689// GetElementPtrInst Implementation
1690//===----------------------------------------------------------------------===//
1691
Pete Cooper1d078692015-12-14 20:29:16 +00001692void GetElementPtrInst::anchor() {}
1693
Jay Foadd1b78492011-07-25 09:48:08 +00001694void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001695 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001696 assert(getNumOperands() == 1 + IdxList.size() &&
1697 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001698 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001699 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001700 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001701}
1702
Gabor Greiff6caff662008-05-10 08:32:32 +00001703GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001704 : Instruction(GEPI.getType(), GetElementPtr,
1705 OperandTraits<GetElementPtrInst>::op_end(this) -
1706 GEPI.getNumOperands(),
1707 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001708 SourceElementType(GEPI.SourceElementType),
1709 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001710 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001711 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001712}
1713
Chris Lattner6090a422009-03-09 04:46:40 +00001714/// getIndexedType - Returns the type of the element that would be accessed with
1715/// a gep instruction with the specified parameters.
1716///
1717/// The Idxs pointer should point to a continuous piece of memory containing the
1718/// indices, either as Value* or uint64_t.
1719///
1720/// A null type is returned if the indices are invalid for the specified
1721/// pointer type.
1722///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001723template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001724static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001725 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001726 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001727 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001728
Chris Lattner6090a422009-03-09 04:46:40 +00001729 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001730 // it cannot be 'stepped over'.
1731 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001732 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001733
Dan Gohman1ecaf452008-05-31 00:58:22 +00001734 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001735 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001736 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001737 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001738 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001739 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001740 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001741 }
Craig Topperc6207612014-04-09 06:08:46 +00001742 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001743}
1744
David Blaikied288fb82015-03-30 21:41:43 +00001745Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1746 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001747}
1748
David Blaikied288fb82015-03-30 21:41:43 +00001749Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001750 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001751 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001752}
1753
David Blaikied288fb82015-03-30 21:41:43 +00001754Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1755 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001756}
1757
Chris Lattner45f15572007-04-14 00:12:57 +00001758/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1759/// zeros. If so, the result pointer and the first operand have the same
1760/// value, just potentially different types.
1761bool GetElementPtrInst::hasAllZeroIndices() const {
1762 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1763 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1764 if (!CI->isZero()) return false;
1765 } else {
1766 return false;
1767 }
1768 }
1769 return true;
1770}
1771
Chris Lattner27058292007-04-27 20:35:56 +00001772/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1773/// constant integers. If so, the result pointer and the first operand have
1774/// a constant offset between them.
1775bool GetElementPtrInst::hasAllConstantIndices() const {
1776 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1777 if (!isa<ConstantInt>(getOperand(i)))
1778 return false;
1779 }
1780 return true;
1781}
1782
Dan Gohman1b849082009-09-07 23:54:19 +00001783void GetElementPtrInst::setIsInBounds(bool B) {
1784 cast<GEPOperator>(this)->setIsInBounds(B);
1785}
Chris Lattner45f15572007-04-14 00:12:57 +00001786
Nick Lewycky28a5f252009-09-27 21:33:04 +00001787bool GetElementPtrInst::isInBounds() const {
1788 return cast<GEPOperator>(this)->isInBounds();
1789}
1790
Chandler Carruth1e140532012-12-11 10:29:10 +00001791bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1792 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001793 // Delegate to the generic GEPOperator implementation.
1794 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001795}
1796
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001797//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001798// ExtractElementInst Implementation
1799//===----------------------------------------------------------------------===//
1800
1801ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001802 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001803 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001804 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001805 ExtractElement,
1806 OperandTraits<ExtractElementInst>::op_begin(this),
1807 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001808 assert(isValidOperands(Val, Index) &&
1809 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001810 Op<0>() = Val;
1811 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001812 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001813}
1814
1815ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001816 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001817 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001818 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001819 ExtractElement,
1820 OperandTraits<ExtractElementInst>::op_begin(this),
1821 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001822 assert(isValidOperands(Val, Index) &&
1823 "Invalid extractelement instruction operands!");
1824
Gabor Greif2d3024d2008-05-26 21:33:52 +00001825 Op<0>() = Val;
1826 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001827 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001828}
1829
Chris Lattner54865b32006-04-08 04:05:48 +00001830bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001831 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001832 return false;
1833 return true;
1834}
1835
Robert Bocchino23004482006-01-10 19:05:34 +00001836//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001837// InsertElementInst Implementation
1838//===----------------------------------------------------------------------===//
1839
Chris Lattner54865b32006-04-08 04:05:48 +00001840InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001841 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001842 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001843 : Instruction(Vec->getType(), InsertElement,
1844 OperandTraits<InsertElementInst>::op_begin(this),
1845 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001846 assert(isValidOperands(Vec, Elt, Index) &&
1847 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001848 Op<0>() = Vec;
1849 Op<1>() = Elt;
1850 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001851 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001852}
1853
Chris Lattner54865b32006-04-08 04:05:48 +00001854InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001855 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001856 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001857 : Instruction(Vec->getType(), InsertElement,
1858 OperandTraits<InsertElementInst>::op_begin(this),
1859 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001860 assert(isValidOperands(Vec, Elt, Index) &&
1861 "Invalid insertelement instruction operands!");
1862
Gabor Greif2d3024d2008-05-26 21:33:52 +00001863 Op<0>() = Vec;
1864 Op<1>() = Elt;
1865 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001866 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001867}
1868
Chris Lattner54865b32006-04-08 04:05:48 +00001869bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1870 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001871 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001872 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001873
Reid Spencerd84d35b2007-02-15 02:26:10 +00001874 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001875 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001876
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001877 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001878 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001879 return true;
1880}
1881
Robert Bocchinoca27f032006-01-17 20:07:22 +00001882//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001883// ShuffleVectorInst Implementation
1884//===----------------------------------------------------------------------===//
1885
1886ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001887 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001888 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001889: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001890 cast<VectorType>(Mask->getType())->getNumElements()),
1891 ShuffleVector,
1892 OperandTraits<ShuffleVectorInst>::op_begin(this),
1893 OperandTraits<ShuffleVectorInst>::operands(this),
1894 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001895 assert(isValidOperands(V1, V2, Mask) &&
1896 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001897 Op<0>() = V1;
1898 Op<1>() = V2;
1899 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001900 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001901}
1902
1903ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001904 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001905 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001906: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1907 cast<VectorType>(Mask->getType())->getNumElements()),
1908 ShuffleVector,
1909 OperandTraits<ShuffleVectorInst>::op_begin(this),
1910 OperandTraits<ShuffleVectorInst>::operands(this),
1911 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001912 assert(isValidOperands(V1, V2, Mask) &&
1913 "Invalid shuffle vector instruction operands!");
1914
Gabor Greif2d3024d2008-05-26 21:33:52 +00001915 Op<0>() = V1;
1916 Op<1>() = V2;
1917 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001918 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001919}
1920
Mon P Wang25f01062008-11-10 04:46:22 +00001921bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001922 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001923 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001924 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001925 return false;
1926
Chris Lattner1dcb6542012-01-25 23:49:49 +00001927 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001928 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001929 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001930 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001931
1932 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001933 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1934 return true;
1935
Sanjay Patel8bd52282017-04-19 16:22:19 +00001936 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001937 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001938 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001939 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001940 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001941 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001942 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001943 return false;
1944 }
1945 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001946 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001947 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001948
Sanjay Patel8bd52282017-04-19 16:22:19 +00001949 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001950 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1951 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1952 if (CDS->getElementAsInteger(i) >= V1Size*2)
1953 return false;
1954 return true;
1955 }
1956
1957 // The bitcode reader can create a place holder for a forward reference
1958 // used as the shuffle mask. When this occurs, the shuffle mask will
1959 // fall into this case and fail. To avoid this error, do this bit of
1960 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001961 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001962 if (CE->getOpcode() == Instruction::UserOp1)
1963 return true;
1964
1965 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001966}
1967
Chris Lattnercf129702012-01-26 02:51:13 +00001968int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1969 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001970 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001971 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001972 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001973 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001974 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001975 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001976}
1977
Chris Lattnercf129702012-01-26 02:51:13 +00001978void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1979 SmallVectorImpl<int> &Result) {
1980 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001981
Sanjay Patel8bd52282017-04-19 16:22:19 +00001982 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001983 for (unsigned i = 0; i != NumElts; ++i)
1984 Result.push_back(CDS->getElementAsInteger(i));
1985 return;
1986 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001987 for (unsigned i = 0; i != NumElts; ++i) {
1988 Constant *C = Mask->getAggregateElement(i);
1989 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001990 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001991 }
1992}
1993
Dan Gohman12fce772008-05-15 19:50:34 +00001994//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001995// InsertValueInst Class
1996//===----------------------------------------------------------------------===//
1997
Jay Foad57aa6362011-07-13 10:26:04 +00001998void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1999 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00002000 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00002001
2002 // There's no fundamental reason why we require at least one index
2003 // (other than weirdness with &*IdxBegin being invalid; see
2004 // getelementptr's init routine for example). But there's no
2005 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002006 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00002007
2008 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00002009 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00002010 Op<0>() = Agg;
2011 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00002012
Jay Foad57aa6362011-07-13 10:26:04 +00002013 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00002014 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002015}
2016
2017InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00002018 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002019 OperandTraits<InsertValueInst>::op_begin(this), 2),
2020 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00002021 Op<0>() = IVI.getOperand(0);
2022 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002023 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002024}
2025
2026//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00002027// ExtractValueInst Class
2028//===----------------------------------------------------------------------===//
2029
Jay Foad57aa6362011-07-13 10:26:04 +00002030void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00002031 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00002032
Jay Foad57aa6362011-07-13 10:26:04 +00002033 // There's no fundamental reason why we require at least one index.
2034 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002035 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00002036
Jay Foad57aa6362011-07-13 10:26:04 +00002037 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00002038 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002039}
2040
2041ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00002042 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00002043 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002044 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002045}
2046
Dan Gohman12fce772008-05-15 19:50:34 +00002047// getIndexedType - Returns the type of the element that would be extracted
2048// with an extractvalue instruction with the specified parameters.
2049//
2050// A null type is returned if the indices are invalid for the specified
2051// pointer type.
2052//
Chris Lattner229907c2011-07-18 04:54:35 +00002053Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00002054 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00002055 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002056 // We can't use CompositeType::indexValid(Index) here.
2057 // indexValid() always returns true for arrays because getelementptr allows
2058 // out-of-bounds indices. Since we don't allow those for extractvalue and
2059 // insertvalue we need to check array indexing manually.
2060 // Since the only other types we can index into are struct types it's just
2061 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002062 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002063 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002064 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002065 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002066 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002067 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002068 } else {
2069 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002070 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002071 }
2072
2073 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002074 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002075 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002076}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002077
2078//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002079// BinaryOperator Class
2080//===----------------------------------------------------------------------===//
2081
Chris Lattner2195fc42007-02-24 00:55:48 +00002082BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002083 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002084 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002085 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002086 OperandTraits<BinaryOperator>::op_begin(this),
2087 OperandTraits<BinaryOperator>::operands(this),
2088 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002089 Op<0>() = S1;
2090 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002091 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002092 setName(Name);
2093}
2094
2095BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002096 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002097 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002098 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002099 OperandTraits<BinaryOperator>::op_begin(this),
2100 OperandTraits<BinaryOperator>::operands(this),
2101 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002102 Op<0>() = S1;
2103 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002104 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002105 setName(Name);
2106}
2107
Chris Lattner2195fc42007-02-24 00:55:48 +00002108void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002109 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002110 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002111 assert(LHS->getType() == RHS->getType() &&
2112 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002113#ifndef NDEBUG
2114 switch (iType) {
2115 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002116 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002117 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002118 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002119 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002120 "Tried to create an integer operation on a non-integer type!");
2121 break;
2122 case FAdd: case FSub:
2123 case FMul:
2124 assert(getType() == LHS->getType() &&
2125 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002126 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002127 "Tried to create a floating-point operation on a "
2128 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002129 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002130 case UDiv:
2131 case SDiv:
2132 assert(getType() == LHS->getType() &&
2133 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002134 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002135 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002136 "Incorrect operand type (not integer) for S/UDIV");
2137 break;
2138 case FDiv:
2139 assert(getType() == LHS->getType() &&
2140 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002141 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002142 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002143 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002144 case URem:
2145 case SRem:
2146 assert(getType() == LHS->getType() &&
2147 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002148 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002149 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002150 "Incorrect operand type (not integer) for S/UREM");
2151 break;
2152 case FRem:
2153 assert(getType() == LHS->getType() &&
2154 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002155 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002156 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002157 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002158 case Shl:
2159 case LShr:
2160 case AShr:
2161 assert(getType() == LHS->getType() &&
2162 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002163 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002164 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002165 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002166 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002167 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002168 case And: case Or:
2169 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002170 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002171 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002172 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002173 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002174 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002175 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002176 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002177 default:
2178 break;
2179 }
2180#endif
2181}
2182
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002183BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002184 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002185 Instruction *InsertBefore) {
2186 assert(S1->getType() == S2->getType() &&
2187 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002188 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002189}
2190
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002191BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002192 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002193 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002194 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002195 InsertAtEnd->getInstList().push_back(Res);
2196 return Res;
2197}
2198
Dan Gohman5476cfd2009-08-12 16:23:25 +00002199BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002200 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002201 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002202 return new BinaryOperator(Instruction::Sub,
2203 zero, Op,
2204 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002205}
2206
Dan Gohman5476cfd2009-08-12 16:23:25 +00002207BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002208 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002209 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002210 return new BinaryOperator(Instruction::Sub,
2211 zero, Op,
2212 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002213}
2214
Dan Gohman4ab44202009-12-18 02:58:50 +00002215BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2216 Instruction *InsertBefore) {
2217 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2218 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2219}
2220
2221BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2222 BasicBlock *InsertAtEnd) {
2223 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2224 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2225}
2226
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002227BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2228 Instruction *InsertBefore) {
2229 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2230 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2231}
2232
2233BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2234 BasicBlock *InsertAtEnd) {
2235 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2236 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2237}
2238
Dan Gohman5476cfd2009-08-12 16:23:25 +00002239BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002240 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002241 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002242 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002243 Op->getType(), Name, InsertBefore);
2244}
2245
Dan Gohman5476cfd2009-08-12 16:23:25 +00002246BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002247 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002248 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002249 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002250 Op->getType(), Name, InsertAtEnd);
2251}
2252
Dan Gohman5476cfd2009-08-12 16:23:25 +00002253BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002254 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002255 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002256 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002257 Op->getType(), Name, InsertBefore);
2258}
2259
Dan Gohman5476cfd2009-08-12 16:23:25 +00002260BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002261 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002262 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002263 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002264 Op->getType(), Name, InsertAtEnd);
2265}
2266
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002267// isConstantAllOnes - Helper function for several functions below
2268static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002269 if (const Constant *C = dyn_cast<Constant>(V))
2270 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002271 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002272}
2273
Owen Andersonbb2501b2009-07-13 22:18:28 +00002274bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002275 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2276 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002277 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002278 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002279 return false;
2280}
2281
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002282bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002283 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2284 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002285 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002286 if (!IgnoreZeroSign)
2287 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2288 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2289 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002290 return false;
2291}
2292
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002293bool BinaryOperator::isNot(const Value *V) {
2294 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2295 return (Bop->getOpcode() == Instruction::Xor &&
2296 (isConstantAllOnes(Bop->getOperand(1)) ||
2297 isConstantAllOnes(Bop->getOperand(0))));
2298 return false;
2299}
2300
Chris Lattner2c7d1772005-04-24 07:28:37 +00002301Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002302 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002303}
2304
Chris Lattner2c7d1772005-04-24 07:28:37 +00002305const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2306 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002307}
2308
Dan Gohmana5b96452009-06-04 22:49:04 +00002309Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002310 return cast<BinaryOperator>(BinOp)->getOperand(1);
2311}
2312
2313const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2314 return getFNegArgument(const_cast<Value*>(BinOp));
2315}
2316
Chris Lattner2c7d1772005-04-24 07:28:37 +00002317Value *BinaryOperator::getNotArgument(Value *BinOp) {
2318 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2319 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2320 Value *Op0 = BO->getOperand(0);
2321 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002322 if (isConstantAllOnes(Op0)) return Op1;
2323
2324 assert(isConstantAllOnes(Op1));
2325 return Op0;
2326}
2327
Chris Lattner2c7d1772005-04-24 07:28:37 +00002328const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2329 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002330}
2331
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002332// Exchange the two operands to this instruction. This instruction is safe to
2333// use on any binary instruction and does not modify the semantics of the
2334// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2335// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002336bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002337 if (!isCommutative())
2338 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002339 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002340 return false;
2341}
2342
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002343//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002344// FPMathOperator Class
2345//===----------------------------------------------------------------------===//
2346
Duncan Sands05f4df82012-04-16 16:28:59 +00002347float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002348 const MDNode *MD =
2349 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002350 if (!MD)
2351 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002352 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002353 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002354}
2355
Duncan Sands05f4df82012-04-16 16:28:59 +00002356//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002357// CastInst Class
2358//===----------------------------------------------------------------------===//
2359
David Blaikie3a15e142011-12-01 08:00:17 +00002360void CastInst::anchor() {}
2361
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362// Just determine if this cast only deals with integral->integral conversion.
2363bool CastInst::isIntegerCast() const {
2364 switch (getOpcode()) {
2365 default: return false;
2366 case Instruction::ZExt:
2367 case Instruction::SExt:
2368 case Instruction::Trunc:
2369 return true;
2370 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002371 return getOperand(0)->getType()->isIntegerTy() &&
2372 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002374}
2375
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002376bool CastInst::isLosslessCast() const {
2377 // Only BitCast can be lossless, exit fast if we're not BitCast
2378 if (getOpcode() != Instruction::BitCast)
2379 return false;
2380
2381 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002382 Type *SrcTy = getOperand(0)->getType();
2383 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384 if (SrcTy == DstTy)
2385 return true;
2386
Reid Spencer8d9336d2006-12-31 05:26:44 +00002387 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002388 if (SrcTy->isPointerTy())
2389 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390 return false; // Other types have no identity values
2391}
2392
2393/// This function determines if the CastInst does not require any bits to be
2394/// changed in order to effect the cast. Essentially, it identifies cases where
2395/// no code gen is necessary for the cast, hence the name no-op cast. For
2396/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002397/// # bitcast i32* %x to i8*
2398/// # bitcast <2 x i32> %x to <4 x i16>
2399/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002400/// @brief Determine if the described cast is a no-op.
2401bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002402 Type *SrcTy,
2403 Type *DestTy,
2404 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002405 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002406 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407 case Instruction::Trunc:
2408 case Instruction::ZExt:
2409 case Instruction::SExt:
2410 case Instruction::FPTrunc:
2411 case Instruction::FPExt:
2412 case Instruction::UIToFP:
2413 case Instruction::SIToFP:
2414 case Instruction::FPToUI:
2415 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002416 case Instruction::AddrSpaceCast:
2417 // TODO: Target informations may give a more accurate answer here.
2418 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419 case Instruction::BitCast:
2420 return true; // BitCast never modifies bits.
2421 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002422 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002423 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002425 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002426 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427 }
2428}
2429
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002430/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002431bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002432 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2433}
2434
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002435bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002436 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002437 if (getOpcode() == Instruction::PtrToInt)
2438 PtrOpTy = getOperand(0)->getType();
2439 else if (getOpcode() == Instruction::IntToPtr)
2440 PtrOpTy = getType();
2441
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002442 Type *IntPtrTy =
2443 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002444
2445 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2446}
2447
2448/// This function determines if a pair of casts can be eliminated and what
2449/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450/// instructions like this:
2451/// * %F = firstOpcode SrcTy %x to MidTy
2452/// * %S = secondOpcode MidTy %F to DstTy
2453/// The function returns a resultOpcode so these two casts can be replaced with:
2454/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002455/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456unsigned CastInst::isEliminableCastPair(
2457 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002458 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2459 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460 // Define the 144 possibilities for these two cast instructions. The values
2461 // in this matrix determine what to do in a given situation and select the
2462 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002463 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 // the following cast properties:
2465 //
2466 // Size Compare Source Destination
2467 // Operator Src ? Size Type Sign Type Sign
2468 // -------- ------------ ------------------- ---------------------
2469 // TRUNC > Integer Any Integral Any
2470 // ZEXT < Integral Unsigned Integer Any
2471 // SEXT < Integral Signed Integer Any
2472 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002473 // FPTOSI n/a FloatPt n/a Integral Signed
2474 // UITOFP n/a Integral Unsigned FloatPt n/a
2475 // SITOFP n/a Integral Signed FloatPt n/a
2476 // FPTRUNC > FloatPt n/a FloatPt n/a
2477 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 // PTRTOINT n/a Pointer n/a Integral Unsigned
2479 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002480 // BITCAST = FirstClass n/a FirstClass n/a
2481 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002482 //
2483 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002484 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2485 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002486 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002487 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002488 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002489 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002490 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2492 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002493 // T F F U S F F P I B A -+
2494 // R Z S P P I I T P 2 N T S |
2495 // U E E 2 2 2 2 R E I T C C +- secondOp
2496 // N X X U S F F N X N 2 V V |
2497 // C T T I I P P C T T P T T -+
2498 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002499 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002500 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2501 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2502 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2503 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2504 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002505 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002506 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2507 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2508 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2509 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2510 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002512
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002513 // TODO: This logic could be encoded into the table above and handled in the
2514 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002515 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002516 // merging. However, any pair of bitcasts are allowed.
2517 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2518 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2519 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002520
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002521 // Check if any of the casts convert scalars <-> vectors.
2522 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2523 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2524 if (!AreBothBitcasts)
2525 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526
2527 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2528 [secondOp-Instruction::CastOpsBegin];
2529 switch (ElimCase) {
2530 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002531 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532 return 0;
2533 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002534 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535 return firstOp;
2536 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002537 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 return secondOp;
2539 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002540 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002541 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002542 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002543 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544 return firstOp;
2545 return 0;
2546 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002547 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002548 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002549 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550 return firstOp;
2551 return 0;
2552 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002553 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002554 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002555 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556 return secondOp;
2557 return 0;
2558 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002559 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002560 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002561 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562 return secondOp;
2563 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002564 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002565 // Cannot simplify if address spaces are different!
2566 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2567 return 0;
2568
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002569 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002570 // We can still fold this without knowing the actual sizes as long we
2571 // know that the intermediate pointer is the largest possible
2572 // pointer size.
2573 // FIXME: Is this always true?
2574 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002575 return Instruction::BitCast;
2576
2577 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002578 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002579 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002580 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581 if (MidSize >= PtrSize)
2582 return Instruction::BitCast;
2583 return 0;
2584 }
2585 case 8: {
2586 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2587 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2588 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002589 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2590 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591 if (SrcSize == DstSize)
2592 return Instruction::BitCast;
2593 else if (SrcSize < DstSize)
2594 return firstOp;
2595 return secondOp;
2596 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002597 case 9:
2598 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599 return Instruction::ZExt;
2600 case 10:
2601 // fpext followed by ftrunc is allowed if the bit size returned to is
2602 // the same as the original, in which case its just a bitcast
2603 if (SrcTy == DstTy)
2604 return Instruction::BitCast;
2605 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002606 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002608 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002609 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002610 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002611 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2612 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613 if (SrcSize <= PtrSize && SrcSize == DstSize)
2614 return Instruction::BitCast;
2615 return 0;
2616 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002617 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002618 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2619 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2620 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2621 return Instruction::AddrSpaceCast;
2622 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002623 case 13:
2624 // FIXME: this state can be merged with (1), but the following assert
2625 // is useful to check the correcteness of the sequence due to semantic
2626 // change of bitcast.
2627 assert(
2628 SrcTy->isPtrOrPtrVectorTy() &&
2629 MidTy->isPtrOrPtrVectorTy() &&
2630 DstTy->isPtrOrPtrVectorTy() &&
2631 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2632 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2633 "Illegal addrspacecast, bitcast sequence!");
2634 // Allowed, use first cast's opcode
2635 return firstOp;
2636 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002637 // bitcast, addrspacecast -> addrspacecast if the element type of
2638 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002639 if (SrcTy->getScalarType()->getPointerElementType() ==
2640 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002641 return Instruction::AddrSpaceCast;
2642 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002643 case 15:
2644 // FIXME: this state can be merged with (1), but the following assert
2645 // is useful to check the correcteness of the sequence due to semantic
2646 // change of bitcast.
2647 assert(
2648 SrcTy->isIntOrIntVectorTy() &&
2649 MidTy->isPtrOrPtrVectorTy() &&
2650 DstTy->isPtrOrPtrVectorTy() &&
2651 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2652 "Illegal inttoptr, bitcast sequence!");
2653 // Allowed, use first cast's opcode
2654 return firstOp;
2655 case 16:
2656 // FIXME: this state can be merged with (2), but the following assert
2657 // is useful to check the correcteness of the sequence due to semantic
2658 // change of bitcast.
2659 assert(
2660 SrcTy->isPtrOrPtrVectorTy() &&
2661 MidTy->isPtrOrPtrVectorTy() &&
2662 DstTy->isIntOrIntVectorTy() &&
2663 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2664 "Illegal bitcast, ptrtoint sequence!");
2665 // Allowed, use second cast's opcode
2666 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002667 case 17:
2668 // (sitofp (zext x)) -> (uitofp x)
2669 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002670 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002671 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002672 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002673 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674 default:
Craig Topperc514b542012-02-05 22:14:15 +00002675 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002676 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677}
2678
Chris Lattner229907c2011-07-18 04:54:35 +00002679CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002680 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002681 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682 // Construct and return the appropriate CastInst subclass
2683 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002684 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2685 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2686 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2687 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2688 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2689 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2690 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2691 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2692 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2693 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2694 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2695 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2696 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2697 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002698 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002699}
2700
Chris Lattner229907c2011-07-18 04:54:35 +00002701CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002702 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002703 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002704 // Construct and return the appropriate CastInst subclass
2705 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002706 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2707 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2708 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2709 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2710 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2711 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2712 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2713 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2714 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2715 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2716 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2717 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2718 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2719 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002720 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002721}
2722
Chris Lattner229907c2011-07-18 04:54:35 +00002723CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002724 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002725 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002726 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002727 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2728 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002729}
2730
Chris Lattner229907c2011-07-18 04:54:35 +00002731CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002732 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002733 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002734 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002735 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2736 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002737}
2738
Chris Lattner229907c2011-07-18 04:54:35 +00002739CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002740 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002741 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002742 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002743 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2744 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002745}
2746
Chris Lattner229907c2011-07-18 04:54:35 +00002747CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002748 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002749 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002750 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002751 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2752 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002753}
2754
Chris Lattner229907c2011-07-18 04:54:35 +00002755CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002756 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002757 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002758 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002759 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2760 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002761}
2762
Chris Lattner229907c2011-07-18 04:54:35 +00002763CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002764 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002765 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002766 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002767 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2768 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002769}
2770
Chris Lattner229907c2011-07-18 04:54:35 +00002771CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002772 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002773 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002774 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2775 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2776 "Invalid cast");
2777 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002778 assert((!Ty->isVectorTy() ||
2779 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002780 "Invalid cast");
2781
Matt Arsenault065ced92013-07-31 00:17:33 +00002782 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002783 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002784
Matt Arsenault740980e2014-07-14 17:24:35 +00002785 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002786}
2787
2788/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002789CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2790 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002791 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002792 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2793 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002794 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002795 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002796 assert((!Ty->isVectorTy() ||
2797 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002798 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002799
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002800 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002801 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002802
Matt Arsenault740980e2014-07-14 17:24:35 +00002803 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2804}
2805
2806CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2807 Value *S, Type *Ty,
2808 const Twine &Name,
2809 BasicBlock *InsertAtEnd) {
2810 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2811 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2812
2813 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2814 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2815
2816 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2817}
2818
2819CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2820 Value *S, Type *Ty,
2821 const Twine &Name,
2822 Instruction *InsertBefore) {
2823 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2824 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2825
2826 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002827 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2828
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002829 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002830}
2831
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002832CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2833 const Twine &Name,
2834 Instruction *InsertBefore) {
2835 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2836 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2837 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2838 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2839
2840 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2841}
2842
Matt Arsenault740980e2014-07-14 17:24:35 +00002843CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002844 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002845 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002846 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002847 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002848 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2849 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002850 Instruction::CastOps opcode =
2851 (SrcBits == DstBits ? Instruction::BitCast :
2852 (SrcBits > DstBits ? Instruction::Trunc :
2853 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002854 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002855}
2856
Chris Lattner229907c2011-07-18 04:54:35 +00002857CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002858 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002859 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002860 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002861 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002862 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2863 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002864 Instruction::CastOps opcode =
2865 (SrcBits == DstBits ? Instruction::BitCast :
2866 (SrcBits > DstBits ? Instruction::Trunc :
2867 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002868 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002869}
2870
Chris Lattner229907c2011-07-18 04:54:35 +00002871CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002872 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002873 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002874 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002875 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002876 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2877 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002878 Instruction::CastOps opcode =
2879 (SrcBits == DstBits ? Instruction::BitCast :
2880 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002881 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002882}
2883
Chris Lattner229907c2011-07-18 04:54:35 +00002884CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002885 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002886 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002887 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002888 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002889 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2890 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002891 Instruction::CastOps opcode =
2892 (SrcBits == DstBits ? Instruction::BitCast :
2893 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002894 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002895}
2896
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002897// Check whether it is valid to call getCastOpcode for these types.
2898// This routine must be kept in sync with getCastOpcode.
2899bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2900 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2901 return false;
2902
2903 if (SrcTy == DestTy)
2904 return true;
2905
2906 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2907 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2908 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2909 // An element by element cast. Valid if casting the elements is valid.
2910 SrcTy = SrcVecTy->getElementType();
2911 DestTy = DestVecTy->getElementType();
2912 }
2913
2914 // Get the bit sizes, we'll need these
2915 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2916 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2917
2918 // Run through the possibilities ...
2919 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002920 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002921 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002922 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002923 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002924 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002925 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002926 // Casting from something else
2927 return SrcTy->isPointerTy();
2928 }
2929 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2930 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002931 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002932 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002933 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002934 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002935 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002936 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002937 return false;
2938 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002939 if (DestTy->isVectorTy()) // Casting to vector
2940 return DestBits == SrcBits;
2941 if (DestTy->isPointerTy()) { // Casting to pointer
2942 if (SrcTy->isPointerTy()) // Casting from pointer
2943 return true;
2944 return SrcTy->isIntegerTy(); // Casting from integral
2945 }
2946 if (DestTy->isX86_MMXTy()) {
2947 if (SrcTy->isVectorTy())
2948 return DestBits == SrcBits; // 64-bit vector to MMX
2949 return false;
2950 } // Casting to something else
2951 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002952}
2953
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002954bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2955 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2956 return false;
2957
2958 if (SrcTy == DestTy)
2959 return true;
2960
2961 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2962 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2963 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2964 // An element by element cast. Valid if casting the elements is valid.
2965 SrcTy = SrcVecTy->getElementType();
2966 DestTy = DestVecTy->getElementType();
2967 }
2968 }
2969 }
2970
2971 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2972 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2973 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2974 }
2975 }
2976
2977 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2978 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2979
2980 // Could still have vectors of pointers if the number of elements doesn't
2981 // match
2982 if (SrcBits == 0 || DestBits == 0)
2983 return false;
2984
2985 if (SrcBits != DestBits)
2986 return false;
2987
2988 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2989 return false;
2990
2991 return true;
2992}
2993
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002994bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002995 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002996 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2997 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002998 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002999 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
3000 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003001 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00003002
3003 return isBitCastable(SrcTy, DestTy);
3004}
3005
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003006// Provide a way to get a "cast" where the cast opcode is inferred from the
3007// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003008// logic in the castIsValid function below. This axiom should hold:
3009// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
3010// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00003012// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003013Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00003014CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00003015 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
3016 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017
Duncan Sands55e50902008-01-06 10:12:28 +00003018 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
3019 "Only first class types are castable!");
3020
Duncan Sandsa8514532011-05-18 07:13:41 +00003021 if (SrcTy == DestTy)
3022 return BitCast;
3023
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003024 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00003025 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3026 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00003027 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
3028 // An element by element cast. Find the appropriate opcode based on the
3029 // element types.
3030 SrcTy = SrcVecTy->getElementType();
3031 DestTy = DestVecTy->getElementType();
3032 }
3033
3034 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00003035 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3036 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00003037
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003038 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00003039 if (DestTy->isIntegerTy()) { // Casting to integral
3040 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003041 if (DestBits < SrcBits)
3042 return Trunc; // int -> smaller int
3043 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00003044 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003045 return SExt; // signed -> SEXT
3046 else
3047 return ZExt; // unsigned -> ZEXT
3048 } else {
3049 return BitCast; // Same size, No-op cast
3050 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003051 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00003052 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003053 return FPToSI; // FP -> sint
3054 else
3055 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003056 } else if (SrcTy->isVectorTy()) {
3057 assert(DestBits == SrcBits &&
3058 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003059 return BitCast; // Same size, no-op cast
3060 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003061 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003062 "Casting from a value that is not first-class type");
3063 return PtrToInt; // ptr -> int
3064 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003065 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3066 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003067 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003068 return SIToFP; // sint -> FP
3069 else
3070 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003071 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003072 if (DestBits < SrcBits) {
3073 return FPTrunc; // FP -> smaller FP
3074 } else if (DestBits > SrcBits) {
3075 return FPExt; // FP -> larger FP
3076 } else {
3077 return BitCast; // same size, no-op cast
3078 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003079 } else if (SrcTy->isVectorTy()) {
3080 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003081 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003082 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003083 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003084 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003085 } else if (DestTy->isVectorTy()) {
3086 assert(DestBits == SrcBits &&
3087 "Illegal cast to vector (wrong type or size)");
3088 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003089 } else if (DestTy->isPointerTy()) {
3090 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003091 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3092 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003093 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003094 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003095 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003096 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003097 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003098 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003099 if (SrcTy->isVectorTy()) {
3100 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003101 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003102 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003103 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003104 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003105 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003106}
3107
3108//===----------------------------------------------------------------------===//
3109// CastInst SubClass Constructors
3110//===----------------------------------------------------------------------===//
3111
3112/// Check that the construction parameters for a CastInst are correct. This
3113/// could be broken out into the separate constructors but it is useful to have
3114/// it in one place and to eliminate the redundant code for getting the sizes
3115/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003116bool
Chris Lattner229907c2011-07-18 04:54:35 +00003117CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003118 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003119 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003120
Chris Lattner37bc78a2010-01-26 21:51:43 +00003121 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3122 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003123 return false;
3124
3125 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003126 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3127 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003128
Duncan Sands7f646562011-05-18 09:21:57 +00003129 // If these are vector types, get the lengths of the vectors (using zero for
3130 // scalar types means that checking that vector lengths match also checks that
3131 // scalars are not being converted to vectors or vectors to scalars).
3132 unsigned SrcLength = SrcTy->isVectorTy() ?
3133 cast<VectorType>(SrcTy)->getNumElements() : 0;
3134 unsigned DstLength = DstTy->isVectorTy() ?
3135 cast<VectorType>(DstTy)->getNumElements() : 0;
3136
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003137 // Switch on the opcode provided
3138 switch (op) {
3139 default: return false; // This is an input error
3140 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003141 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3142 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003144 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3145 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003146 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003147 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3148 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003149 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003150 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3151 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003152 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003153 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3154 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003155 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003156 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003157 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3158 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003159 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003160 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003161 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3162 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003163 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003164 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003165 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003166 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3167 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3168 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003169 return SrcTy->getScalarType()->isPointerTy() &&
3170 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003171 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003172 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003173 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003174 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3175 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3176 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003177 return SrcTy->getScalarType()->isIntegerTy() &&
3178 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003179 case Instruction::BitCast: {
3180 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3181 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3182
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003183 // BitCast implies a no-op cast of type only. No bits change.
3184 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003185 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186 return false;
3187
Alp Tokerf907b892013-12-05 05:44:44 +00003188 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003189 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003190 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003191 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3192
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003193 // If both are pointers then the address spaces must match.
3194 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3195 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003196
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003197 // A vector of pointers must have the same number of elements.
3198 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3199 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3200 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3201
3202 return false;
3203 }
3204
3205 return true;
3206 }
3207 case Instruction::AddrSpaceCast: {
3208 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3209 if (!SrcPtrTy)
3210 return false;
3211
3212 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3213 if (!DstPtrTy)
3214 return false;
3215
3216 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3217 return false;
3218
3219 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3220 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3221 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3222
3223 return false;
3224 }
3225
3226 return true;
3227 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228 }
3229}
3230
3231TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003232 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003233) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003234 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003235}
3236
3237TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003238 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003239) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003241}
3242
3243ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003244 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003245) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003246 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003247}
3248
3249ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003250 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003251) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003252 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003253}
3254SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003255 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003256) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003257 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258}
3259
Jeff Cohencc08c832006-12-02 02:22:01 +00003260SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003261 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003262) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003263 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264}
3265
3266FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003267 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003268) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003269 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270}
3271
3272FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003273 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003274) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003275 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276}
3277
3278FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003279 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003280) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003281 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282}
3283
3284FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003285 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003286) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003287 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003288}
3289
3290UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003291 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003292) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003293 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003294}
3295
3296UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003297 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003298) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003299 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003300}
3301
3302SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003303 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003304) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003305 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003306}
3307
3308SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003309 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003310) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003311 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003312}
3313
3314FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003315 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003316) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003317 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003318}
3319
3320FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003321 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003322) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003323 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003324}
3325
3326FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003327 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003328) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003329 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003330}
3331
3332FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003333 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003334) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003335 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003336}
3337
3338PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003339 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003340) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003341 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003342}
3343
3344PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003345 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003346) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003347 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003348}
3349
3350IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003351 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003352) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003353 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003354}
3355
3356IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003357 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003358) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003359 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003360}
3361
3362BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003363 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003364) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003365 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003366}
3367
3368BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003369 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003370) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003371 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003372}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003373
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003374AddrSpaceCastInst::AddrSpaceCastInst(
3375 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3376) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3377 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3378}
3379
3380AddrSpaceCastInst::AddrSpaceCastInst(
3381 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3382) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3383 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3384}
3385
Chris Lattnerf16dc002006-09-17 19:29:56 +00003386//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003387// CmpInst Classes
3388//===----------------------------------------------------------------------===//
3389
Craig Topper3186c012012-09-23 02:12:10 +00003390void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003391
Craig Topper1c3f2832015-12-15 06:11:33 +00003392CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3393 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003394 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003395 OperandTraits<CmpInst>::op_begin(this),
3396 OperandTraits<CmpInst>::operands(this),
3397 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003398 Op<0>() = LHS;
3399 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003400 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003401 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003402}
Gabor Greiff6caff662008-05-10 08:32:32 +00003403
Craig Topper1c3f2832015-12-15 06:11:33 +00003404CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3405 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003406 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003407 OperandTraits<CmpInst>::op_begin(this),
3408 OperandTraits<CmpInst>::operands(this),
3409 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003410 Op<0>() = LHS;
3411 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003412 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003413 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003414}
3415
3416CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003417CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003418 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003419 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003420 if (InsertBefore)
3421 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3422 S1, S2, Name);
3423 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003424 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003425 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003426 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003427
3428 if (InsertBefore)
3429 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3430 S1, S2, Name);
3431 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003432 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003433 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003434}
3435
3436CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003437CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003438 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003439 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003440 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3441 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003442 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003443 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3444 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003445}
3446
3447void CmpInst::swapOperands() {
3448 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3449 IC->swapOperands();
3450 else
3451 cast<FCmpInst>(this)->swapOperands();
3452}
3453
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003454bool CmpInst::isCommutative() const {
3455 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003456 return IC->isCommutative();
3457 return cast<FCmpInst>(this)->isCommutative();
3458}
3459
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003460bool CmpInst::isEquality() const {
3461 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003462 return IC->isEquality();
3463 return cast<FCmpInst>(this)->isEquality();
3464}
3465
Dan Gohman4e724382008-05-31 02:47:54 +00003466CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003467 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003468 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003469 case ICMP_EQ: return ICMP_NE;
3470 case ICMP_NE: return ICMP_EQ;
3471 case ICMP_UGT: return ICMP_ULE;
3472 case ICMP_ULT: return ICMP_UGE;
3473 case ICMP_UGE: return ICMP_ULT;
3474 case ICMP_ULE: return ICMP_UGT;
3475 case ICMP_SGT: return ICMP_SLE;
3476 case ICMP_SLT: return ICMP_SGE;
3477 case ICMP_SGE: return ICMP_SLT;
3478 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003479
Dan Gohman4e724382008-05-31 02:47:54 +00003480 case FCMP_OEQ: return FCMP_UNE;
3481 case FCMP_ONE: return FCMP_UEQ;
3482 case FCMP_OGT: return FCMP_ULE;
3483 case FCMP_OLT: return FCMP_UGE;
3484 case FCMP_OGE: return FCMP_ULT;
3485 case FCMP_OLE: return FCMP_UGT;
3486 case FCMP_UEQ: return FCMP_ONE;
3487 case FCMP_UNE: return FCMP_OEQ;
3488 case FCMP_UGT: return FCMP_OLE;
3489 case FCMP_ULT: return FCMP_OGE;
3490 case FCMP_UGE: return FCMP_OLT;
3491 case FCMP_ULE: return FCMP_OGT;
3492 case FCMP_ORD: return FCMP_UNO;
3493 case FCMP_UNO: return FCMP_ORD;
3494 case FCMP_TRUE: return FCMP_FALSE;
3495 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003496 }
3497}
3498
Tim Northoverde3aea0412016-08-17 20:25:25 +00003499StringRef CmpInst::getPredicateName(Predicate Pred) {
3500 switch (Pred) {
3501 default: return "unknown";
3502 case FCmpInst::FCMP_FALSE: return "false";
3503 case FCmpInst::FCMP_OEQ: return "oeq";
3504 case FCmpInst::FCMP_OGT: return "ogt";
3505 case FCmpInst::FCMP_OGE: return "oge";
3506 case FCmpInst::FCMP_OLT: return "olt";
3507 case FCmpInst::FCMP_OLE: return "ole";
3508 case FCmpInst::FCMP_ONE: return "one";
3509 case FCmpInst::FCMP_ORD: return "ord";
3510 case FCmpInst::FCMP_UNO: return "uno";
3511 case FCmpInst::FCMP_UEQ: return "ueq";
3512 case FCmpInst::FCMP_UGT: return "ugt";
3513 case FCmpInst::FCMP_UGE: return "uge";
3514 case FCmpInst::FCMP_ULT: return "ult";
3515 case FCmpInst::FCMP_ULE: return "ule";
3516 case FCmpInst::FCMP_UNE: return "une";
3517 case FCmpInst::FCMP_TRUE: return "true";
3518 case ICmpInst::ICMP_EQ: return "eq";
3519 case ICmpInst::ICMP_NE: return "ne";
3520 case ICmpInst::ICMP_SGT: return "sgt";
3521 case ICmpInst::ICMP_SGE: return "sge";
3522 case ICmpInst::ICMP_SLT: return "slt";
3523 case ICmpInst::ICMP_SLE: return "sle";
3524 case ICmpInst::ICMP_UGT: return "ugt";
3525 case ICmpInst::ICMP_UGE: return "uge";
3526 case ICmpInst::ICMP_ULT: return "ult";
3527 case ICmpInst::ICMP_ULE: return "ule";
3528 }
3529}
3530
Pete Cooper1d078692015-12-14 20:29:16 +00003531void ICmpInst::anchor() {}
3532
Reid Spencer266e42b2006-12-23 06:05:41 +00003533ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3534 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003535 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003536 case ICMP_EQ: case ICMP_NE:
3537 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3538 return pred;
3539 case ICMP_UGT: return ICMP_SGT;
3540 case ICMP_ULT: return ICMP_SLT;
3541 case ICMP_UGE: return ICMP_SGE;
3542 case ICMP_ULE: return ICMP_SLE;
3543 }
3544}
3545
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003546ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3547 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003548 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003549 case ICMP_EQ: case ICMP_NE:
3550 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3551 return pred;
3552 case ICMP_SGT: return ICMP_UGT;
3553 case ICMP_SLT: return ICMP_ULT;
3554 case ICMP_SGE: return ICMP_UGE;
3555 case ICMP_SLE: return ICMP_ULE;
3556 }
3557}
3558
Dan Gohman4e724382008-05-31 02:47:54 +00003559CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003560 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003561 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003562 case ICMP_EQ: case ICMP_NE:
3563 return pred;
3564 case ICMP_SGT: return ICMP_SLT;
3565 case ICMP_SLT: return ICMP_SGT;
3566 case ICMP_SGE: return ICMP_SLE;
3567 case ICMP_SLE: return ICMP_SGE;
3568 case ICMP_UGT: return ICMP_ULT;
3569 case ICMP_ULT: return ICMP_UGT;
3570 case ICMP_UGE: return ICMP_ULE;
3571 case ICMP_ULE: return ICMP_UGE;
3572
Reid Spencerd9436b62006-11-20 01:22:35 +00003573 case FCMP_FALSE: case FCMP_TRUE:
3574 case FCMP_OEQ: case FCMP_ONE:
3575 case FCMP_UEQ: case FCMP_UNE:
3576 case FCMP_ORD: case FCMP_UNO:
3577 return pred;
3578 case FCMP_OGT: return FCMP_OLT;
3579 case FCMP_OLT: return FCMP_OGT;
3580 case FCMP_OGE: return FCMP_OLE;
3581 case FCMP_OLE: return FCMP_OGE;
3582 case FCMP_UGT: return FCMP_ULT;
3583 case FCMP_ULT: return FCMP_UGT;
3584 case FCMP_UGE: return FCMP_ULE;
3585 case FCMP_ULE: return FCMP_UGE;
3586 }
3587}
3588
Sanjoy Das6e78b172015-10-22 19:57:34 +00003589CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3590 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3591
3592 switch (pred) {
3593 default:
3594 llvm_unreachable("Unknown predicate!");
3595 case CmpInst::ICMP_ULT:
3596 return CmpInst::ICMP_SLT;
3597 case CmpInst::ICMP_ULE:
3598 return CmpInst::ICMP_SLE;
3599 case CmpInst::ICMP_UGT:
3600 return CmpInst::ICMP_SGT;
3601 case CmpInst::ICMP_UGE:
3602 return CmpInst::ICMP_SGE;
3603 }
3604}
3605
Craig Topper1c3f2832015-12-15 06:11:33 +00003606bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003607 switch (predicate) {
3608 default: return false;
3609 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3610 case ICmpInst::ICMP_UGE: return true;
3611 }
3612}
3613
Craig Topper1c3f2832015-12-15 06:11:33 +00003614bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003615 switch (predicate) {
3616 default: return false;
3617 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3618 case ICmpInst::ICMP_SGE: return true;
3619 }
3620}
3621
Craig Topper1c3f2832015-12-15 06:11:33 +00003622bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003623 switch (predicate) {
3624 default: return false;
3625 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3626 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3627 case FCmpInst::FCMP_ORD: return true;
3628 }
3629}
3630
Craig Topper1c3f2832015-12-15 06:11:33 +00003631bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003632 switch (predicate) {
3633 default: return false;
3634 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3635 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3636 case FCmpInst::FCMP_UNO: return true;
3637 }
3638}
3639
Craig Topper1c3f2832015-12-15 06:11:33 +00003640bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003641 switch(predicate) {
3642 default: return false;
3643 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3644 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3645 }
3646}
3647
Craig Topper1c3f2832015-12-15 06:11:33 +00003648bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003649 switch(predicate) {
3650 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3651 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3652 default: return false;
3653 }
3654}
3655
Chad Rosier99bc4802016-04-21 16:18:02 +00003656bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003657 // If the predicates match, then we know the first condition implies the
3658 // second is true.
3659 if (Pred1 == Pred2)
3660 return true;
3661
3662 switch (Pred1) {
3663 default:
3664 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003665 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003666 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
Chad Rosier1a601592016-04-22 17:57:34 +00003667 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3668 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003669 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3670 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3671 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3672 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3673 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3674 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3675 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3676 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003677 }
3678 return false;
3679}
3680
Chad Rosier99bc4802016-04-21 16:18:02 +00003681bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003682 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003683}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003684
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003685//===----------------------------------------------------------------------===//
3686// SwitchInst Implementation
3687//===----------------------------------------------------------------------===//
3688
Chris Lattnerbaf00152010-11-17 05:41:46 +00003689void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3690 assert(Value && Default && NumReserved);
3691 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003692 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003693 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003694
Pete Coopereb31b682015-05-21 22:48:54 +00003695 Op<0>() = Value;
3696 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003697}
3698
Chris Lattner2195fc42007-02-24 00:55:48 +00003699/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3700/// switch on and a default destination. The number of additional cases can
3701/// be specified here to make memory allocation more efficient. This
3702/// constructor can also autoinsert before another instruction.
3703SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3704 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003705 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003706 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003707 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003708}
3709
3710/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3711/// switch on and a default destination. The number of additional cases can
3712/// be specified here to make memory allocation more efficient. This
3713/// constructor also autoinserts at the end of the specified BasicBlock.
3714SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3715 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003716 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003717 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003718 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003719}
3720
Misha Brukmanb1c93172005-04-21 23:48:37 +00003721SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003722 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003723 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003724 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003725 Use *OL = getOperandList();
3726 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003727 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003728 OL[i] = InOL[i];
3729 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003730 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003731 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003732}
3733
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003734
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003735/// addCase - Add an entry to the switch instruction...
3736///
Chris Lattner47ac1872005-02-24 05:32:09 +00003737void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003738 unsigned NewCaseIdx = getNumCases();
3739 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003740 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003741 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003742 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003743 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003744 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003745 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003746 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003747 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003748}
3749
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003750/// removeCase - This method removes the specified case and its successor
3751/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003752SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3753 unsigned idx = I->getCaseIndex();
3754
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003755 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003756
3757 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003758 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003759
Jay Foad14277722011-02-01 09:22:34 +00003760 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003761 if (2 + (idx + 1) * 2 != NumOps) {
3762 OL[2 + idx * 2] = OL[NumOps - 2];
3763 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003764 }
3765
3766 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003767 OL[NumOps-2].set(nullptr);
3768 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003769 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003770
3771 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003772}
3773
Jay Foade98f29d2011-04-01 08:00:58 +00003774/// growOperands - grow operands - This grows the operand list in response
3775/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003776///
Jay Foade98f29d2011-04-01 08:00:58 +00003777void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003778 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003779 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003780
3781 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003782 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003783}
3784
3785
3786BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3787 return getSuccessor(idx);
3788}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00003789
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003790unsigned SwitchInst::getNumSuccessorsV() const {
3791 return getNumSuccessors();
3792}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00003793
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003794void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3795 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003796}
Chris Lattnerf22be932004-10-15 23:52:53 +00003797
Chris Lattner3ed871f2009-10-27 19:13:16 +00003798//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003799// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003800//===----------------------------------------------------------------------===//
3801
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003802void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003803 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003804 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003805 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003806 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003807 allocHungoffUses(ReservedSpace);
3808
Pete Coopereb31b682015-05-21 22:48:54 +00003809 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003810}
3811
3812
Jay Foade98f29d2011-04-01 08:00:58 +00003813/// growOperands - grow operands - This grows the operand list in response
3814/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003815///
Jay Foade98f29d2011-04-01 08:00:58 +00003816void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003817 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003818 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003819
3820 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003821 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003822}
3823
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003824IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3825 Instruction *InsertBefore)
3826: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003827 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003828 init(Address, NumCases);
3829}
3830
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003831IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3832 BasicBlock *InsertAtEnd)
3833: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003834 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003835 init(Address, NumCases);
3836}
3837
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003838IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003839 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3840 nullptr, IBI.getNumOperands()) {
3841 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003842 Use *OL = getOperandList();
3843 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003844 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3845 OL[i] = InOL[i];
3846 SubclassOptionalData = IBI.SubclassOptionalData;
3847}
3848
Chris Lattner3ed871f2009-10-27 19:13:16 +00003849/// addDestination - Add a destination.
3850///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003851void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003852 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003853 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003854 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003855 // Initialize some new operands.
3856 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003857 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003858 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003859}
3860
3861/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003862/// indirectbr instruction.
3863void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003864 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3865
3866 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003867 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003868
3869 // Replace this value with the last one.
3870 OL[idx+1] = OL[NumOps-1];
3871
3872 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003873 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003874 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003875}
3876
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003877BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003878 return getSuccessor(idx);
3879}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00003880
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003881unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003882 return getNumSuccessors();
3883}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00003884
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003885void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003886 setSuccessor(idx, B);
3887}
3888
3889//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003890// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003891//===----------------------------------------------------------------------===//
3892
Chris Lattnerf22be932004-10-15 23:52:53 +00003893// Define these methods here so vtables don't get emitted into every translation
3894// unit that uses these classes.
3895
Pete Cooper75403d72015-06-24 20:22:23 +00003896GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003897 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003898}
3899
Pete Cooper75403d72015-06-24 20:22:23 +00003900BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003901 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003902}
3903
Pete Cooper75403d72015-06-24 20:22:23 +00003904FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003905 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003906}
3907
Pete Cooper75403d72015-06-24 20:22:23 +00003908ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003909 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003910}
3911
Pete Cooper75403d72015-06-24 20:22:23 +00003912ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003913 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003914}
3915
Pete Cooper75403d72015-06-24 20:22:23 +00003916InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003917 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003918}
3919
Pete Cooper75403d72015-06-24 20:22:23 +00003920AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003921 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003922 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003923 (Value *)getOperand(0), getAlignment());
3924 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003925 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003926 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003927}
3928
Pete Cooper75403d72015-06-24 20:22:23 +00003929LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003930 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3931 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003932}
3933
Pete Cooper75403d72015-06-24 20:22:23 +00003934StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003935 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003936 getAlignment(), getOrdering(), getSynchScope());
3937
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003938}
3939
Pete Cooper75403d72015-06-24 20:22:23 +00003940AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003941 AtomicCmpXchgInst *Result =
3942 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003943 getSuccessOrdering(), getFailureOrdering(),
3944 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003945 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003946 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003947 return Result;
3948}
3949
Pete Cooper75403d72015-06-24 20:22:23 +00003950AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003951 AtomicRMWInst *Result =
3952 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3953 getOrdering(), getSynchScope());
3954 Result->setVolatile(isVolatile());
3955 return Result;
3956}
3957
Pete Cooper75403d72015-06-24 20:22:23 +00003958FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003959 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3960}
3961
Pete Cooper75403d72015-06-24 20:22:23 +00003962TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003963 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003964}
3965
Pete Cooper75403d72015-06-24 20:22:23 +00003966ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003967 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003968}
3969
Pete Cooper75403d72015-06-24 20:22:23 +00003970SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003971 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003972}
3973
Pete Cooper75403d72015-06-24 20:22:23 +00003974FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003975 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003976}
3977
Pete Cooper75403d72015-06-24 20:22:23 +00003978FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003979 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003980}
3981
Pete Cooper75403d72015-06-24 20:22:23 +00003982UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003983 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003984}
3985
Pete Cooper75403d72015-06-24 20:22:23 +00003986SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003987 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003988}
3989
Pete Cooper75403d72015-06-24 20:22:23 +00003990FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003991 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003992}
3993
Pete Cooper75403d72015-06-24 20:22:23 +00003994FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003995 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003996}
3997
Pete Cooper75403d72015-06-24 20:22:23 +00003998PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003999 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004000}
4001
Pete Cooper75403d72015-06-24 20:22:23 +00004002IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004003 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004004}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004005
Pete Cooper75403d72015-06-24 20:22:23 +00004006BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004007 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00004008}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00004009
Pete Cooper75403d72015-06-24 20:22:23 +00004010AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004011 return new AddrSpaceCastInst(getOperand(0), getType());
4012}
4013
Pete Cooper75403d72015-06-24 20:22:23 +00004014CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004015 if (hasOperandBundles()) {
4016 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4017 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
4018 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004019 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004020}
4021
Pete Cooper75403d72015-06-24 20:22:23 +00004022SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004023 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004024}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004025
Pete Cooper75403d72015-06-24 20:22:23 +00004026VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004027 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004028}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004029
Pete Cooper75403d72015-06-24 20:22:23 +00004030ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004031 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004032}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004033
Pete Cooper75403d72015-06-24 20:22:23 +00004034InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004035 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004036}
4037
Pete Cooper75403d72015-06-24 20:22:23 +00004038ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004039 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00004040}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004041
Pete Cooper75403d72015-06-24 20:22:23 +00004042PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00004043
Pete Cooper75403d72015-06-24 20:22:23 +00004044LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00004045 return new LandingPadInst(*this);
4046}
4047
Pete Cooper75403d72015-06-24 20:22:23 +00004048ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004049 return new(getNumOperands()) ReturnInst(*this);
4050}
4051
Pete Cooper75403d72015-06-24 20:22:23 +00004052BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004053 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004054}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004055
Pete Cooper75403d72015-06-24 20:22:23 +00004056SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004057
Pete Cooper75403d72015-06-24 20:22:23 +00004058IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004059 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004060}
4061
Pete Cooper75403d72015-06-24 20:22:23 +00004062InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004063 if (hasOperandBundles()) {
4064 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4065 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4066 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004067 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004068}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004069
Pete Cooper75403d72015-06-24 20:22:23 +00004070ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004071
David Majnemer654e1302015-07-31 17:58:14 +00004072CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4073 return new (getNumOperands()) CleanupReturnInst(*this);
4074}
4075
David Majnemer654e1302015-07-31 17:58:14 +00004076CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004077 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004078}
4079
David Majnemer8a1c45d2015-12-12 05:38:55 +00004080CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
4081 return new CatchSwitchInst(*this);
4082}
4083
4084FuncletPadInst *FuncletPadInst::cloneImpl() const {
4085 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004086}
4087
Pete Cooper75403d72015-06-24 20:22:23 +00004088UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004089 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004090 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004091}