blob: 59500992abb4ee81431b1a5a810916424a8b405c [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
Pete Cooper1d078692015-12-14 20:29:16 +000090void PHINode::anchor() {}
91
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000093 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
94 ReservedSpace(PN.getNumOperands()) {
95 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000096 std::copy(PN.op_begin(), PN.op_end(), op_begin());
97 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101// removeIncomingValue - Remove an incoming value. This is useful if a
102// predecessor basic block is deleted.
103Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105
106 // Move everything after this operand down.
107 //
108 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
112 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000113
114 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000115 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000116 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117
118 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000121 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 eraseFromParent();
123 }
124 return Removed;
125}
126
Jay Foade98f29d2011-04-01 08:00:58 +0000127/// growOperands - grow operands - This grows the operand list in response
128/// to a push_back style of operation. This grows the number of ops by 1.5
129/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130///
Jay Foade98f29d2011-04-01 08:00:58 +0000131void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000132 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000133 unsigned NumOps = e + e / 2;
134 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
135
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000137 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Nate Begemanb3923212005-08-04 23:24:19 +0000140/// hasConstantValue - If the specified PHI node always merges together the same
141/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000142Value *PHINode::hasConstantValue() const {
143 // Exploit the fact that phi nodes always have at least one entry.
144 Value *ConstantValue = getIncomingValue(0);
145 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000146 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
147 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000148 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000149 // The case where the first value is this PHI.
150 ConstantValue = getIncomingValue(i);
151 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000152 if (ConstantValue == this)
153 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000155}
156
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000157/// hasConstantOrUndefValue - Whether the specified PHI node always merges
158/// together the same value, assuming that undefs result in the same value as
159/// non-undefs.
160/// Unlike \ref hasConstantValue, this does not return a value because the
161/// unique non-undef incoming value need not dominate the PHI node.
162bool PHINode::hasConstantOrUndefValue() const {
163 Value *ConstantValue = nullptr;
164 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
165 Value *Incoming = getIncomingValue(i);
166 if (Incoming != this && !isa<UndefValue>(Incoming)) {
167 if (ConstantValue && ConstantValue != Incoming)
168 return false;
169 ConstantValue = Incoming;
170 }
171 }
172 return true;
173}
174
Bill Wendlingfae14752011-08-12 20:24:12 +0000175//===----------------------------------------------------------------------===//
176// LandingPadInst Implementation
177//===----------------------------------------------------------------------===//
178
David Majnemer7fddecc2015-06-17 20:52:32 +0000179LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
180 const Twine &NameStr, Instruction *InsertBefore)
181 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
182 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000183}
184
David Majnemer7fddecc2015-06-17 20:52:32 +0000185LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
186 const Twine &NameStr, BasicBlock *InsertAtEnd)
187 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
188 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000189}
190
191LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000192 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
193 LP.getNumOperands()),
194 ReservedSpace(LP.getNumOperands()) {
195 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000196 Use *OL = getOperandList();
197 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000198 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
199 OL[I] = InOL[I];
200
201 setCleanup(LP.isCleanup());
202}
203
David Majnemer7fddecc2015-06-17 20:52:32 +0000204LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000205 const Twine &NameStr,
206 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000207 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000208}
209
David Majnemer7fddecc2015-06-17 20:52:32 +0000210LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000211 const Twine &NameStr,
212 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000213 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000214}
215
David Majnemer7fddecc2015-06-17 20:52:32 +0000216void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000217 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000218 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000219 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000220 setName(NameStr);
221 setCleanup(false);
222}
223
224/// growOperands - grow operands - This grows the operand list in response to a
225/// push_back style of operation. This grows the number of ops by 2 times.
226void LandingPadInst::growOperands(unsigned Size) {
227 unsigned e = getNumOperands();
228 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000229 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000230 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000231}
232
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000233void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000234 unsigned OpNo = getNumOperands();
235 growOperands(1);
236 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000237 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000238 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000239}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240
241//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242// CallInst Implementation
243//===----------------------------------------------------------------------===//
244
Gordon Henriksen14a55692007-12-10 02:14:30 +0000245CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246}
247
David Blaikie348de692015-04-23 21:36:23 +0000248void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000249 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000250 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000251 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
252 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000253 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254
Jay Foad5bd375a2011-07-15 08:37:34 +0000255#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000256 assert((Args.size() == FTy->getNumParams() ||
257 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000258 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000259
260 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000261 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000262 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000263 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000264#endif
265
266 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000267
268 auto It = populateBundleOperandInfos(Bundles, Args.size());
269 (void)It;
270 assert(It + 1 == op_end() && "Should add up!");
271
Jay Foad5bd375a2011-07-15 08:37:34 +0000272 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273}
274
Jay Foad5bd375a2011-07-15 08:37:34 +0000275void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000276 FTy =
277 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000278 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000279 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000282
283 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284}
285
Daniel Dunbar4975db62009-07-25 04:41:11 +0000286CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 Instruction *InsertBefore)
288 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
289 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000290 Instruction::Call,
291 OperandTraits<CallInst>::op_end(this) - 1,
292 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000293 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294}
295
Daniel Dunbar4975db62009-07-25 04:41:11 +0000296CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297 BasicBlock *InsertAtEnd)
298 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
299 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000300 Instruction::Call,
301 OperandTraits<CallInst>::op_end(this) - 1,
302 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000303 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304}
305
Misha Brukmanb1c93172005-04-21 23:48:37 +0000306CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000307 : Instruction(CI.getType(), Instruction::Call,
308 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
309 CI.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000310 Attrs(CI.Attrs), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000311 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000312 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000313
Jay Foad5bd375a2011-07-15 08:37:34 +0000314 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000315 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
316 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000317 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Sanjoy Das2d161452015-11-18 06:23:38 +0000320CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
321 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000322 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000323
324 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
325 InsertPt);
326 NewCI->setTailCallKind(CI->getTailCallKind());
327 NewCI->setCallingConv(CI->getCallingConv());
328 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000329 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000330 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000331 return NewCI;
332}
333
Hal Finkele87ad542016-07-10 23:01:32 +0000334Value *CallInst::getReturnedArgOperand() const {
335 unsigned Index;
336
Reid Klecknerb5180542017-03-21 16:57:19 +0000337 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Hal Finkele87ad542016-07-10 23:01:32 +0000338 return getArgOperand(Index-1);
339 if (const Function *F = getCalledFunction())
340 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
341 Index)
342 return getArgOperand(Index-1);
343
344 return nullptr;
345}
346
Amaury Sechet392638d2016-06-14 20:27:35 +0000347void CallInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000348 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000349 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000350 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000351}
352
Amaury Secheta65a2372016-06-15 05:14:29 +0000353void CallInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000354 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000355 PAL = PAL.addAttribute(getContext(), i, Attr);
356 setAttributes(PAL);
357}
358
Amaury Sechet392638d2016-06-14 20:27:35 +0000359void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000360 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000361 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000362 setAttributes(PAL);
363}
364
Amaury Sechet6100adf2016-06-15 17:50:39 +0000365void CallInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000366 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000367 PAL = PAL.removeAttribute(getContext(), i, Kind);
368 setAttributes(PAL);
369}
370
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000371void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000372 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000373 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
374 setAttributes(PAL);
375}
376
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000377void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000378 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000379 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
380 setAttributes(PAL);
381}
382
Reid Klecknerfb502d22017-04-14 20:19:02 +0000383bool CallInst::hasRetAttr(Attribute::AttrKind Kind) const {
384 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
385 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000386
Reid Klecknerfb502d22017-04-14 20:19:02 +0000387 // Look at the callee, if available.
388 if (const Function *F = getCalledFunction())
389 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
390 return false;
391}
392
393bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
394 assert(i < getNumArgOperands() && "Param index out of bounds!");
395
396 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000397 return true;
398 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000399 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000400 return false;
401}
402
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000403bool CallInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000404 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000405 // There are getNumOperands() - 1 data operands. The last operand is the
406 // callee.
407 assert(i < getNumOperands() && "Data operand index out of bounds!");
408
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000409 // The attribute A can either be directly specified, if the operand in
410 // question is a call argument; or be indirectly implied by the kind of its
411 // containing operand bundle, if the operand is a bundle operand.
412
Reid Klecknerfb502d22017-04-14 20:19:02 +0000413 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
414 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000415 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000416 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000417
418 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
419 "Must be either a call argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000420 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000421}
422
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000423/// IsConstantOne - Return true only if val is constant int 1
424static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000425 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000426 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
427 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000428}
429
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000430static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000431 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000432 Type *AllocTy, Value *AllocSize,
433 Value *ArraySize,
434 ArrayRef<OperandBundleDef> OpB,
435 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000436 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000437 "createMalloc needs either InsertBefore or InsertAtEnd");
438
439 // malloc(type) becomes:
440 // bitcast (i8* malloc(typeSize)) to type*
441 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000442 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000443 if (!ArraySize)
444 ArraySize = ConstantInt::get(IntPtrTy, 1);
445 else if (ArraySize->getType() != IntPtrTy) {
446 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000447 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
448 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000449 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000450 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
451 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000452 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000453
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000454 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000455 if (IsConstantOne(AllocSize)) {
456 AllocSize = ArraySize; // Operand * 1 = Operand
457 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
458 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
459 false /*ZExt*/);
460 // Malloc arg is constant product of type size and array size
461 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
462 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000463 // Multiply type size by the array size...
464 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000465 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
466 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000467 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000468 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
469 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000470 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000471 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000472
Victor Hernandez788eaab2009-09-18 19:20:02 +0000473 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000474 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000475 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
476 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000477 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000478 Value *MallocFunc = MallocF;
479 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000480 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000481 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000482 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000483 CallInst *MCall = nullptr;
484 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000485 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000486 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
487 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000488 Result = MCall;
489 if (Result->getType() != AllocPtrType)
490 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000491 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000492 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000493 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000494 Result = MCall;
495 if (Result->getType() != AllocPtrType) {
496 InsertAtEnd->getInstList().push_back(MCall);
497 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000498 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000499 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000500 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000501 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000502 if (Function *F = dyn_cast<Function>(MallocFunc)) {
503 MCall->setCallingConv(F->getCallingConv());
Reid Kleckner6652a522017-04-28 18:37:16 +0000504 if (!F->doesNotAlias(AttributeList::ReturnIndex))
505 F->setDoesNotAlias(AttributeList::ReturnIndex);
Victor Hernandezbb336a12009-11-10 19:53:28 +0000506 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000507 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000508
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000509 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000510}
511
512/// CreateMalloc - Generate the IR for a call to malloc:
513/// 1. Compute the malloc call's argument as the specified type's size,
514/// possibly multiplied by the array size if the array size is not
515/// constant 1.
516/// 2. Call malloc with that argument.
517/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000518Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000519 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000520 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000521 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000522 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000523 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000524 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000525}
David Majnemerfadc6db2016-04-29 08:07:22 +0000526Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
527 Type *IntPtrTy, Type *AllocTy,
528 Value *AllocSize, Value *ArraySize,
529 ArrayRef<OperandBundleDef> OpB,
530 Function *MallocF,
531 const Twine &Name) {
532 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
533 ArraySize, OpB, MallocF, Name);
534}
535
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000536
537/// CreateMalloc - Generate the IR for a call to malloc:
538/// 1. Compute the malloc call's argument as the specified type's size,
539/// possibly multiplied by the array size if the array size is not
540/// constant 1.
541/// 2. Call malloc with that argument.
542/// 3. Bitcast the result of the malloc call to the specified type.
543/// Note: This function does not add the bitcast to the basic block, that is the
544/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000545Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000546 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000547 Value *AllocSize, Value *ArraySize,
548 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000549 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000550 ArraySize, None, MallocF, Name);
551}
552Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
553 Type *IntPtrTy, Type *AllocTy,
554 Value *AllocSize, Value *ArraySize,
555 ArrayRef<OperandBundleDef> OpB,
556 Function *MallocF, const Twine &Name) {
557 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
558 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000559}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000560
David Majnemerfadc6db2016-04-29 08:07:22 +0000561static Instruction *createFree(Value *Source,
562 ArrayRef<OperandBundleDef> Bundles,
563 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000564 BasicBlock *InsertAtEnd) {
565 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
566 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000567 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000568 "Can not free something of nonpointer type!");
569
Ana Pazosb3596022016-02-03 21:34:39 +0000570 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
571 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000572
Chris Lattner229907c2011-07-18 04:54:35 +0000573 Type *VoidTy = Type::getVoidTy(M->getContext());
574 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000575 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000576 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000577 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000578 Value *PtrCast = Source;
579 if (InsertBefore) {
580 if (Source->getType() != IntPtrTy)
581 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000582 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000583 } else {
584 if (Source->getType() != IntPtrTy)
585 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000586 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000587 }
588 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000589 if (Function *F = dyn_cast<Function>(FreeFunc))
590 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000591
592 return Result;
593}
594
595/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000596Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000597 return createFree(Source, None, InsertBefore, nullptr);
598}
599Instruction *CallInst::CreateFree(Value *Source,
600 ArrayRef<OperandBundleDef> Bundles,
601 Instruction *InsertBefore) {
602 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000603}
604
605/// CreateFree - Generate the IR for a call to the builtin free function.
606/// Note: This function does not add the call to the basic block, that is the
607/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000608Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000609 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
610 assert(FreeCall && "CreateFree did not create a CallInst");
611 return FreeCall;
612}
613Instruction *CallInst::CreateFree(Value *Source,
614 ArrayRef<OperandBundleDef> Bundles,
615 BasicBlock *InsertAtEnd) {
616 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000617 assert(FreeCall && "CreateFree did not create a CallInst");
618 return FreeCall;
619}
620
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621//===----------------------------------------------------------------------===//
622// InvokeInst Implementation
623//===----------------------------------------------------------------------===//
624
David Blaikie3e807092015-05-13 18:35:26 +0000625void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
626 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000627 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000628 const Twine &NameStr) {
629 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000630
Sanjoy Das9303c242015-09-24 19:14:18 +0000631 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
632 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000633 Op<-3>() = Fn;
634 Op<-2>() = IfNormal;
635 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000636
637#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000638 assert(((Args.size() == FTy->getNumParams()) ||
639 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000640 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000641
Jay Foad5bd375a2011-07-15 08:37:34 +0000642 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000643 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000644 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000645 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000646#endif
647
648 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000649
650 auto It = populateBundleOperandInfos(Bundles, Args.size());
651 (void)It;
652 assert(It + 3 == op_end() && "Should add up!");
653
Jay Foad5bd375a2011-07-15 08:37:34 +0000654 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000655}
656
Misha Brukmanb1c93172005-04-21 23:48:37 +0000657InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000658 : TerminatorInst(II.getType(), Instruction::Invoke,
659 OperandTraits<InvokeInst>::op_end(this) -
660 II.getNumOperands(),
661 II.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000662 Attrs(II.Attrs), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000663 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000664 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000665 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
666 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000667 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000668}
669
Sanjoy Das2d161452015-11-18 06:23:38 +0000670InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
671 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000672 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000673
674 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
675 II->getUnwindDest(), Args, OpB,
676 II->getName(), InsertPt);
677 NewII->setCallingConv(II->getCallingConv());
678 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000679 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000680 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000681 return NewII;
682}
683
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000684BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
685 return getSuccessor(idx);
686}
687unsigned InvokeInst::getNumSuccessorsV() const {
688 return getNumSuccessors();
689}
690void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
691 return setSuccessor(idx, B);
692}
693
Hal Finkele87ad542016-07-10 23:01:32 +0000694Value *InvokeInst::getReturnedArgOperand() const {
695 unsigned Index;
696
Reid Klecknerb5180542017-03-21 16:57:19 +0000697 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Hal Finkele87ad542016-07-10 23:01:32 +0000698 return getArgOperand(Index-1);
699 if (const Function *F = getCalledFunction())
700 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
701 Index)
702 return getArgOperand(Index-1);
703
704 return nullptr;
705}
706
Reid Klecknerfb502d22017-04-14 20:19:02 +0000707bool InvokeInst::hasRetAttr(Attribute::AttrKind Kind) const {
708 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
709 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000710
Reid Klecknerfb502d22017-04-14 20:19:02 +0000711 // Look at the callee, if available.
712 if (const Function *F = getCalledFunction())
713 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
714 return false;
715}
716
717bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
718 assert(i < getNumArgOperands() && "Param index out of bounds!");
719
720 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000721 return true;
722 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000723 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000724 return false;
725}
726
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000727bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000728 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000729 // There are getNumOperands() - 3 data operands. The last three operands are
730 // the callee and the two successor basic blocks.
731 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
732
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000733 // The attribute A can either be directly specified, if the operand in
734 // question is an invoke argument; or be indirectly implied by the kind of its
735 // containing operand bundle, if the operand is a bundle operand.
736
Reid Klecknerfb502d22017-04-14 20:19:02 +0000737 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
738 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000739 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000740 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000741
742 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
743 "Must be either an invoke argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000744 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000745}
746
Amaury Sechet392638d2016-06-14 20:27:35 +0000747void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000748 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000749 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000750 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000751}
752
Amaury Secheta65a2372016-06-15 05:14:29 +0000753void InvokeInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000754 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000755 PAL = PAL.addAttribute(getContext(), i, Attr);
756 setAttributes(PAL);
757}
758
Amaury Sechet392638d2016-06-14 20:27:35 +0000759void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000760 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000761 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000762 setAttributes(PAL);
763}
764
Amaury Sechet6100adf2016-06-15 17:50:39 +0000765void InvokeInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000766 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000767 PAL = PAL.removeAttribute(getContext(), i, Kind);
768 setAttributes(PAL);
769}
770
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000771void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000772 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000773 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
774 setAttributes(PAL);
775}
776
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000777void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000778 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000779 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
780 setAttributes(PAL);
781}
782
Bill Wendlingfae14752011-08-12 20:24:12 +0000783LandingPadInst *InvokeInst::getLandingPadInst() const {
784 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
785}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000786
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787//===----------------------------------------------------------------------===//
788// ReturnInst Implementation
789//===----------------------------------------------------------------------===//
790
Chris Lattner2195fc42007-02-24 00:55:48 +0000791ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000792 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000793 OperandTraits<ReturnInst>::op_end(this) -
794 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000795 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000796 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000797 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000798 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000799}
800
Owen Anderson55f1c092009-08-13 21:58:54 +0000801ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
802 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000803 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
804 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000805 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000806 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000807}
Owen Anderson55f1c092009-08-13 21:58:54 +0000808ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
809 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000810 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
811 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000812 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000813 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000814}
Owen Anderson55f1c092009-08-13 21:58:54 +0000815ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
816 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000817 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000818}
819
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000820unsigned ReturnInst::getNumSuccessorsV() const {
821 return getNumSuccessors();
822}
823
Devang Patelae682fb2008-02-26 17:56:20 +0000824/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
825/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000826void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000827 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828}
829
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000830BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000831 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000832}
833
Devang Patel59643e52008-02-23 00:35:18 +0000834ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000835}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000836
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000837//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000838// ResumeInst Implementation
839//===----------------------------------------------------------------------===//
840
841ResumeInst::ResumeInst(const ResumeInst &RI)
842 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
843 OperandTraits<ResumeInst>::op_begin(this), 1) {
844 Op<0>() = RI.Op<0>();
845}
846
847ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
848 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
849 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
850 Op<0>() = Exn;
851}
852
853ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
854 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
855 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
856 Op<0>() = Exn;
857}
858
859unsigned ResumeInst::getNumSuccessorsV() const {
860 return getNumSuccessors();
861}
862
863void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
864 llvm_unreachable("ResumeInst has no successors!");
865}
866
867BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
868 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000869}
870
871//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000872// CleanupReturnInst Implementation
873//===----------------------------------------------------------------------===//
874
875CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
876 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
877 OperandTraits<CleanupReturnInst>::op_end(this) -
878 CRI.getNumOperands(),
879 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000880 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000881 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000882 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000883 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000884}
885
David Majnemer8a1c45d2015-12-12 05:38:55 +0000886void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000887 if (UnwindBB)
888 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000889
David Majnemer8a1c45d2015-12-12 05:38:55 +0000890 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000891 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000892 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000893}
894
David Majnemer8a1c45d2015-12-12 05:38:55 +0000895CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
896 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000897 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
898 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000899 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
900 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000901 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000902}
903
David Majnemer8a1c45d2015-12-12 05:38:55 +0000904CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
905 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000906 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
907 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000908 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
909 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000910 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000911}
912
913BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
914 assert(Idx == 0);
915 return getUnwindDest();
916}
917unsigned CleanupReturnInst::getNumSuccessorsV() const {
918 return getNumSuccessors();
919}
920void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
921 assert(Idx == 0);
922 setUnwindDest(B);
923}
924
925//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000926// CatchReturnInst Implementation
927//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000928void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000929 Op<0>() = CatchPad;
930 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000931}
David Majnemer654e1302015-07-31 17:58:14 +0000932
933CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
934 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000935 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
936 Op<0>() = CRI.Op<0>();
937 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000938}
939
David Majnemer8a1c45d2015-12-12 05:38:55 +0000940CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000941 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000942 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000943 OperandTraits<CatchReturnInst>::op_begin(this), 2,
944 InsertBefore) {
945 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000946}
947
David Majnemer8a1c45d2015-12-12 05:38:55 +0000948CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000949 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000950 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000951 OperandTraits<CatchReturnInst>::op_begin(this), 2,
952 InsertAtEnd) {
953 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000954}
955
956BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000957 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000958 return getSuccessor();
959}
960unsigned CatchReturnInst::getNumSuccessorsV() const {
961 return getNumSuccessors();
962}
963void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000964 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000965 setSuccessor(B);
966}
967
968//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000969// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000970//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000971
972CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
973 unsigned NumReservedValues,
974 const Twine &NameStr,
975 Instruction *InsertBefore)
976 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
977 InsertBefore) {
978 if (UnwindDest)
979 ++NumReservedValues;
980 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000981 setName(NameStr);
982}
983
David Majnemer8a1c45d2015-12-12 05:38:55 +0000984CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
985 unsigned NumReservedValues,
986 const Twine &NameStr, BasicBlock *InsertAtEnd)
987 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000988 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000989 if (UnwindDest)
990 ++NumReservedValues;
991 init(ParentPad, UnwindDest, NumReservedValues + 1);
992 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000993}
994
David Majnemer8a1c45d2015-12-12 05:38:55 +0000995CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
996 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
997 CSI.getNumOperands()) {
998 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
999 setNumHungOffUseOperands(ReservedSpace);
1000 Use *OL = getOperandList();
1001 const Use *InOL = CSI.getOperandList();
1002 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
1003 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +00001004}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001005
1006void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
1007 unsigned NumReservedValues) {
1008 assert(ParentPad && NumReservedValues);
1009
1010 ReservedSpace = NumReservedValues;
1011 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
1012 allocHungoffUses(ReservedSpace);
1013
1014 Op<0>() = ParentPad;
1015 if (UnwindDest) {
1016 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1017 setUnwindDest(UnwindDest);
1018 }
1019}
1020
1021/// growOperands - grow operands - This grows the operand list in response to a
1022/// push_back style of operation. This grows the number of ops by 2 times.
1023void CatchSwitchInst::growOperands(unsigned Size) {
1024 unsigned NumOperands = getNumOperands();
1025 assert(NumOperands >= 1);
1026 if (ReservedSpace >= NumOperands + Size)
1027 return;
1028 ReservedSpace = (NumOperands + Size / 2) * 2;
1029 growHungoffUses(ReservedSpace);
1030}
1031
1032void CatchSwitchInst::addHandler(BasicBlock *Handler) {
1033 unsigned OpNo = getNumOperands();
1034 growOperands(1);
1035 assert(OpNo < ReservedSpace && "Growing didn't work!");
1036 setNumHungOffUseOperands(getNumOperands() + 1);
1037 getOperandList()[OpNo] = Handler;
1038}
1039
Joseph Tremoulet0d808882016-01-05 02:37:41 +00001040void CatchSwitchInst::removeHandler(handler_iterator HI) {
1041 // Move all subsequent handlers up one.
1042 Use *EndDst = op_end() - 1;
1043 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
1044 *CurDst = *(CurDst + 1);
1045 // Null out the last handler use.
1046 *EndDst = nullptr;
1047
1048 setNumHungOffUseOperands(getNumOperands() - 1);
1049}
1050
David Majnemer8a1c45d2015-12-12 05:38:55 +00001051BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
1052 return getSuccessor(idx);
1053}
1054unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +00001055 return getNumSuccessors();
1056}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001057void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1058 setSuccessor(idx, B);
1059}
1060
1061//===----------------------------------------------------------------------===//
1062// FuncletPadInst Implementation
1063//===----------------------------------------------------------------------===//
1064void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1065 const Twine &NameStr) {
1066 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
1067 std::copy(Args.begin(), Args.end(), op_begin());
1068 setParentPad(ParentPad);
1069 setName(NameStr);
1070}
1071
1072FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1073 : Instruction(FPI.getType(), FPI.getOpcode(),
1074 OperandTraits<FuncletPadInst>::op_end(this) -
1075 FPI.getNumOperands(),
1076 FPI.getNumOperands()) {
1077 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1078 setParentPad(FPI.getParentPad());
1079}
1080
1081FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1082 ArrayRef<Value *> Args, unsigned Values,
1083 const Twine &NameStr, Instruction *InsertBefore)
1084 : Instruction(ParentPad->getType(), Op,
1085 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1086 InsertBefore) {
1087 init(ParentPad, Args, NameStr);
1088}
1089
1090FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1091 ArrayRef<Value *> Args, unsigned Values,
1092 const Twine &NameStr, BasicBlock *InsertAtEnd)
1093 : Instruction(ParentPad->getType(), Op,
1094 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1095 InsertAtEnd) {
1096 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001097}
1098
1099//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001100// UnreachableInst Implementation
1101//===----------------------------------------------------------------------===//
1102
Owen Anderson55f1c092009-08-13 21:58:54 +00001103UnreachableInst::UnreachableInst(LLVMContext &Context,
1104 Instruction *InsertBefore)
1105 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001106 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001107}
Owen Anderson55f1c092009-08-13 21:58:54 +00001108UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1109 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001110 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001111}
1112
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001113unsigned UnreachableInst::getNumSuccessorsV() const {
1114 return getNumSuccessors();
1115}
1116
1117void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001118 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001119}
1120
1121BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001122 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001123}
1124
1125//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001126// BranchInst Implementation
1127//===----------------------------------------------------------------------===//
1128
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001129void BranchInst::AssertOK() {
1130 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001131 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001132 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001133}
1134
Chris Lattner2195fc42007-02-24 00:55:48 +00001135BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001136 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001137 OperandTraits<BranchInst>::op_end(this) - 1,
1138 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001139 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001140 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001141}
1142BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1143 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001144 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001145 OperandTraits<BranchInst>::op_end(this) - 3,
1146 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001147 Op<-1>() = IfTrue;
1148 Op<-2>() = IfFalse;
1149 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001150#ifndef NDEBUG
1151 AssertOK();
1152#endif
1153}
1154
1155BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001156 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001157 OperandTraits<BranchInst>::op_end(this) - 1,
1158 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001159 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001160 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001161}
1162
1163BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1164 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001165 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001166 OperandTraits<BranchInst>::op_end(this) - 3,
1167 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001168 Op<-1>() = IfTrue;
1169 Op<-2>() = IfFalse;
1170 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001171#ifndef NDEBUG
1172 AssertOK();
1173#endif
1174}
1175
1176
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001177BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001178 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001179 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1180 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001181 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001182 if (BI.getNumOperands() != 1) {
1183 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001184 Op<-3>() = BI.Op<-3>();
1185 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001186 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001187 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001188}
1189
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001190void BranchInst::swapSuccessors() {
1191 assert(isConditional() &&
1192 "Cannot swap successors of an unconditional branch");
1193 Op<-1>().swap(Op<-2>());
1194
1195 // Update profile metadata if present and it matches our structural
1196 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001197 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001198}
1199
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001200BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1201 return getSuccessor(idx);
1202}
1203unsigned BranchInst::getNumSuccessorsV() const {
1204 return getNumSuccessors();
1205}
1206void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1207 setSuccessor(idx, B);
1208}
1209
1210
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001211//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001212// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001213//===----------------------------------------------------------------------===//
1214
Owen Andersonb6b25302009-07-14 23:09:55 +00001215static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001216 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001217 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001218 else {
1219 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001220 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001221 assert(Amt->getType()->isIntegerTy() &&
1222 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001223 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001224 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001225}
1226
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001227AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001228 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001229 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001230
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001231AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001232 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001233 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001234
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001235AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001236 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001237 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1238
1239AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1240 const Twine &Name, BasicBlock *InsertAtEnd)
1241 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1242
1243AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1244 unsigned Align, const Twine &Name,
1245 Instruction *InsertBefore)
1246 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1247 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1248 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001249 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001250 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001251 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001252}
1253
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001254AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1255 unsigned Align, const Twine &Name,
1256 BasicBlock *InsertAtEnd)
1257 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1258 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001259 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001260 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001261 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001262 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001263}
1264
Gordon Henriksen14a55692007-12-10 02:14:30 +00001265// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001266AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001267}
1268
Victor Hernandez8acf2952009-10-23 21:09:37 +00001269void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001270 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001271 assert(Align <= MaximumAlignment &&
1272 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001273 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1274 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001275 assert(getAlignment() == Align && "Alignment representation error!");
1276}
1277
Victor Hernandez8acf2952009-10-23 21:09:37 +00001278bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001279 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001280 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001281 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001282}
1283
Chris Lattner8b291e62008-11-26 02:54:17 +00001284/// isStaticAlloca - Return true if this alloca is in the entry block of the
1285/// function and is a constant size. If so, the code generator will fold it
1286/// into the prolog/epilog code, so it is basically free.
1287bool AllocaInst::isStaticAlloca() const {
1288 // Must be constant size.
1289 if (!isa<ConstantInt>(getArraySize())) return false;
1290
1291 // Must be in the entry block.
1292 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001293 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001294}
1295
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001296//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001297// LoadInst Implementation
1298//===----------------------------------------------------------------------===//
1299
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001300void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001301 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001302 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001303 assert(!(isAtomic() && getAlignment() == 0) &&
1304 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001305}
1306
Daniel Dunbar4975db62009-07-25 04:41:11 +00001307LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001308 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001309
Daniel Dunbar4975db62009-07-25 04:41:11 +00001310LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001311 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001312
David Blaikie0c28fd72015-05-20 21:46:30 +00001313LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001314 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001315 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001316
1317LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1318 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001319 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001320
David Blaikieb7a029872015-04-17 19:56:21 +00001321LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001322 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001323 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1324 CrossThread, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001325
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001326LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001327 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001328 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1329 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001330
David Blaikie15d9a4c2015-04-06 20:59:48 +00001331LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001332 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001333 SynchronizationScope SynchScope, Instruction *InsertBef)
1334 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001335 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001336 setVolatile(isVolatile);
1337 setAlignment(Align);
1338 setAtomic(Order, SynchScope);
1339 AssertOK();
1340 setName(Name);
1341}
1342
1343LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1344 unsigned Align, AtomicOrdering Order,
1345 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001346 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001347 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001348 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001349 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001350 setAlignment(Align);
1351 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001352 AssertOK();
1353 setName(Name);
1354}
1355
Daniel Dunbar27096822009-08-11 18:11:15 +00001356LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1357 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1358 Load, Ptr, InsertBef) {
1359 setVolatile(false);
1360 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001361 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001362 AssertOK();
1363 if (Name && Name[0]) setName(Name);
1364}
1365
1366LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1367 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1368 Load, Ptr, InsertAE) {
1369 setVolatile(false);
1370 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001371 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001372 AssertOK();
1373 if (Name && Name[0]) setName(Name);
1374}
1375
David Blaikie0c28fd72015-05-20 21:46:30 +00001376LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001377 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001378 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1379 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001380 setVolatile(isVolatile);
1381 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001382 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001383 AssertOK();
1384 if (Name && Name[0]) setName(Name);
1385}
1386
1387LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1388 BasicBlock *InsertAE)
1389 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1390 Load, Ptr, InsertAE) {
1391 setVolatile(isVolatile);
1392 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001393 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001394 AssertOK();
1395 if (Name && Name[0]) setName(Name);
1396}
1397
Christopher Lamb84485702007-04-22 19:24:39 +00001398void LoadInst::setAlignment(unsigned Align) {
1399 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001400 assert(Align <= MaximumAlignment &&
1401 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001402 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001403 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001404 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001405}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001406
1407//===----------------------------------------------------------------------===//
1408// StoreInst Implementation
1409//===----------------------------------------------------------------------===//
1410
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001411void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001412 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001413 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001414 "Ptr must have pointer type!");
1415 assert(getOperand(0)->getType() ==
1416 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001417 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001418 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001419 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001420}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001421
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001422StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001423 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001424
1425StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001426 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001427
Misha Brukmanb1c93172005-04-21 23:48:37 +00001428StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001429 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001430 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001431
1432StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001433 BasicBlock *InsertAtEnd)
1434 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1435
1436StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1437 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001438 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1439 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001440
1441StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1442 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001443 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1444 CrossThread, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001445
Misha Brukmanb1c93172005-04-21 23:48:37 +00001446StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001447 unsigned Align, AtomicOrdering Order,
1448 SynchronizationScope SynchScope,
1449 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001450 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001451 OperandTraits<StoreInst>::op_begin(this),
1452 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001453 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001454 Op<0>() = val;
1455 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001456 setVolatile(isVolatile);
1457 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001458 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001459 AssertOK();
1460}
1461
1462StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001463 unsigned Align, AtomicOrdering Order,
1464 SynchronizationScope SynchScope,
1465 BasicBlock *InsertAtEnd)
1466 : Instruction(Type::getVoidTy(val->getContext()), Store,
1467 OperandTraits<StoreInst>::op_begin(this),
1468 OperandTraits<StoreInst>::operands(this),
1469 InsertAtEnd) {
1470 Op<0>() = val;
1471 Op<1>() = addr;
1472 setVolatile(isVolatile);
1473 setAlignment(Align);
1474 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001475 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001476}
1477
Christopher Lamb84485702007-04-22 19:24:39 +00001478void StoreInst::setAlignment(unsigned Align) {
1479 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001480 assert(Align <= MaximumAlignment &&
1481 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001482 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001483 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001484 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001485}
1486
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001487//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001488// AtomicCmpXchgInst Implementation
1489//===----------------------------------------------------------------------===//
1490
1491void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001492 AtomicOrdering SuccessOrdering,
1493 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001494 SynchronizationScope SynchScope) {
1495 Op<0>() = Ptr;
1496 Op<1>() = Cmp;
1497 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001498 setSuccessOrdering(SuccessOrdering);
1499 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001500 setSynchScope(SynchScope);
1501
1502 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1503 "All operands must be non-null!");
1504 assert(getOperand(0)->getType()->isPointerTy() &&
1505 "Ptr must have pointer type!");
1506 assert(getOperand(1)->getType() ==
1507 cast<PointerType>(getOperand(0)->getType())->getElementType()
1508 && "Ptr must be a pointer to Cmp type!");
1509 assert(getOperand(2)->getType() ==
1510 cast<PointerType>(getOperand(0)->getType())->getElementType()
1511 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001512 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001513 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001514 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001515 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001516 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1517 "AtomicCmpXchg failure argument shall be no stronger than the success "
1518 "argument");
1519 assert(FailureOrdering != AtomicOrdering::Release &&
1520 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001521 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001522}
1523
1524AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001525 AtomicOrdering SuccessOrdering,
1526 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001527 SynchronizationScope SynchScope,
1528 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001529 : Instruction(
1530 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1531 nullptr),
1532 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1533 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001534 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001535}
1536
1537AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001538 AtomicOrdering SuccessOrdering,
1539 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001540 SynchronizationScope SynchScope,
1541 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001542 : Instruction(
1543 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1544 nullptr),
1545 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1546 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001547 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001548}
Tim Northover420a2162014-06-13 14:24:07 +00001549
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001550//===----------------------------------------------------------------------===//
1551// AtomicRMWInst Implementation
1552//===----------------------------------------------------------------------===//
1553
1554void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1555 AtomicOrdering Ordering,
1556 SynchronizationScope SynchScope) {
1557 Op<0>() = Ptr;
1558 Op<1>() = Val;
1559 setOperation(Operation);
1560 setOrdering(Ordering);
1561 setSynchScope(SynchScope);
1562
1563 assert(getOperand(0) && getOperand(1) &&
1564 "All operands must be non-null!");
1565 assert(getOperand(0)->getType()->isPointerTy() &&
1566 "Ptr must have pointer type!");
1567 assert(getOperand(1)->getType() ==
1568 cast<PointerType>(getOperand(0)->getType())->getElementType()
1569 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001570 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001571 "AtomicRMW instructions must be atomic!");
1572}
1573
1574AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1575 AtomicOrdering Ordering,
1576 SynchronizationScope SynchScope,
1577 Instruction *InsertBefore)
1578 : Instruction(Val->getType(), AtomicRMW,
1579 OperandTraits<AtomicRMWInst>::op_begin(this),
1580 OperandTraits<AtomicRMWInst>::operands(this),
1581 InsertBefore) {
1582 Init(Operation, Ptr, Val, Ordering, SynchScope);
1583}
1584
1585AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1586 AtomicOrdering Ordering,
1587 SynchronizationScope SynchScope,
1588 BasicBlock *InsertAtEnd)
1589 : Instruction(Val->getType(), AtomicRMW,
1590 OperandTraits<AtomicRMWInst>::op_begin(this),
1591 OperandTraits<AtomicRMWInst>::operands(this),
1592 InsertAtEnd) {
1593 Init(Operation, Ptr, Val, Ordering, SynchScope);
1594}
1595
1596//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001597// FenceInst Implementation
1598//===----------------------------------------------------------------------===//
1599
1600FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1601 SynchronizationScope SynchScope,
1602 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001603 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001604 setOrdering(Ordering);
1605 setSynchScope(SynchScope);
1606}
1607
1608FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1609 SynchronizationScope SynchScope,
1610 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001611 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001612 setOrdering(Ordering);
1613 setSynchScope(SynchScope);
1614}
1615
1616//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617// GetElementPtrInst Implementation
1618//===----------------------------------------------------------------------===//
1619
Pete Cooper1d078692015-12-14 20:29:16 +00001620void GetElementPtrInst::anchor() {}
1621
Jay Foadd1b78492011-07-25 09:48:08 +00001622void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001623 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001624 assert(getNumOperands() == 1 + IdxList.size() &&
1625 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001626 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001627 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001628 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001629}
1630
Gabor Greiff6caff662008-05-10 08:32:32 +00001631GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001632 : Instruction(GEPI.getType(), GetElementPtr,
1633 OperandTraits<GetElementPtrInst>::op_end(this) -
1634 GEPI.getNumOperands(),
1635 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001636 SourceElementType(GEPI.SourceElementType),
1637 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001638 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001639 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001640}
1641
Chris Lattner6090a422009-03-09 04:46:40 +00001642/// getIndexedType - Returns the type of the element that would be accessed with
1643/// a gep instruction with the specified parameters.
1644///
1645/// The Idxs pointer should point to a continuous piece of memory containing the
1646/// indices, either as Value* or uint64_t.
1647///
1648/// A null type is returned if the indices are invalid for the specified
1649/// pointer type.
1650///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001651template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001652static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001653 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001654 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001655 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001656
Chris Lattner6090a422009-03-09 04:46:40 +00001657 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001658 // it cannot be 'stepped over'.
1659 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001660 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001661
Dan Gohman1ecaf452008-05-31 00:58:22 +00001662 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001663 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001664 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001665 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001666 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001667 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001668 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001669 }
Craig Topperc6207612014-04-09 06:08:46 +00001670 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001671}
1672
David Blaikied288fb82015-03-30 21:41:43 +00001673Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1674 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001675}
1676
David Blaikied288fb82015-03-30 21:41:43 +00001677Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001678 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001679 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001680}
1681
David Blaikied288fb82015-03-30 21:41:43 +00001682Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1683 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001684}
1685
Chris Lattner45f15572007-04-14 00:12:57 +00001686/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1687/// zeros. If so, the result pointer and the first operand have the same
1688/// value, just potentially different types.
1689bool GetElementPtrInst::hasAllZeroIndices() const {
1690 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1691 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1692 if (!CI->isZero()) return false;
1693 } else {
1694 return false;
1695 }
1696 }
1697 return true;
1698}
1699
Chris Lattner27058292007-04-27 20:35:56 +00001700/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1701/// constant integers. If so, the result pointer and the first operand have
1702/// a constant offset between them.
1703bool GetElementPtrInst::hasAllConstantIndices() const {
1704 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1705 if (!isa<ConstantInt>(getOperand(i)))
1706 return false;
1707 }
1708 return true;
1709}
1710
Dan Gohman1b849082009-09-07 23:54:19 +00001711void GetElementPtrInst::setIsInBounds(bool B) {
1712 cast<GEPOperator>(this)->setIsInBounds(B);
1713}
Chris Lattner45f15572007-04-14 00:12:57 +00001714
Nick Lewycky28a5f252009-09-27 21:33:04 +00001715bool GetElementPtrInst::isInBounds() const {
1716 return cast<GEPOperator>(this)->isInBounds();
1717}
1718
Chandler Carruth1e140532012-12-11 10:29:10 +00001719bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1720 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001721 // Delegate to the generic GEPOperator implementation.
1722 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001723}
1724
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001725//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001726// ExtractElementInst Implementation
1727//===----------------------------------------------------------------------===//
1728
1729ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001730 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001731 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001732 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001733 ExtractElement,
1734 OperandTraits<ExtractElementInst>::op_begin(this),
1735 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001736 assert(isValidOperands(Val, Index) &&
1737 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001738 Op<0>() = Val;
1739 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001740 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001741}
1742
1743ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001744 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001745 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001746 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001747 ExtractElement,
1748 OperandTraits<ExtractElementInst>::op_begin(this),
1749 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001750 assert(isValidOperands(Val, Index) &&
1751 "Invalid extractelement instruction operands!");
1752
Gabor Greif2d3024d2008-05-26 21:33:52 +00001753 Op<0>() = Val;
1754 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001755 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001756}
1757
Chris Lattner65511ff2006-10-05 06:24:58 +00001758
Chris Lattner54865b32006-04-08 04:05:48 +00001759bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001760 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001761 return false;
1762 return true;
1763}
1764
1765
Robert Bocchino23004482006-01-10 19:05:34 +00001766//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001767// InsertElementInst Implementation
1768//===----------------------------------------------------------------------===//
1769
Chris Lattner54865b32006-04-08 04:05:48 +00001770InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001771 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001772 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001773 : Instruction(Vec->getType(), InsertElement,
1774 OperandTraits<InsertElementInst>::op_begin(this),
1775 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001776 assert(isValidOperands(Vec, Elt, Index) &&
1777 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001778 Op<0>() = Vec;
1779 Op<1>() = Elt;
1780 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001781 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001782}
1783
Chris Lattner54865b32006-04-08 04:05:48 +00001784InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001785 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001786 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001787 : Instruction(Vec->getType(), InsertElement,
1788 OperandTraits<InsertElementInst>::op_begin(this),
1789 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001790 assert(isValidOperands(Vec, Elt, Index) &&
1791 "Invalid insertelement instruction operands!");
1792
Gabor Greif2d3024d2008-05-26 21:33:52 +00001793 Op<0>() = Vec;
1794 Op<1>() = Elt;
1795 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001796 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001797}
1798
Chris Lattner54865b32006-04-08 04:05:48 +00001799bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1800 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001801 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001802 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001803
Reid Spencerd84d35b2007-02-15 02:26:10 +00001804 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001805 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001806
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001807 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001808 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001809 return true;
1810}
1811
1812
Robert Bocchinoca27f032006-01-17 20:07:22 +00001813//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001814// ShuffleVectorInst Implementation
1815//===----------------------------------------------------------------------===//
1816
1817ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001818 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001819 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001820: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001821 cast<VectorType>(Mask->getType())->getNumElements()),
1822 ShuffleVector,
1823 OperandTraits<ShuffleVectorInst>::op_begin(this),
1824 OperandTraits<ShuffleVectorInst>::operands(this),
1825 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001826 assert(isValidOperands(V1, V2, Mask) &&
1827 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001828 Op<0>() = V1;
1829 Op<1>() = V2;
1830 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001831 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001832}
1833
1834ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001835 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001836 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001837: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1838 cast<VectorType>(Mask->getType())->getNumElements()),
1839 ShuffleVector,
1840 OperandTraits<ShuffleVectorInst>::op_begin(this),
1841 OperandTraits<ShuffleVectorInst>::operands(this),
1842 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001843 assert(isValidOperands(V1, V2, Mask) &&
1844 "Invalid shuffle vector instruction operands!");
1845
Gabor Greif2d3024d2008-05-26 21:33:52 +00001846 Op<0>() = V1;
1847 Op<1>() = V2;
1848 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001849 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001850}
1851
Mon P Wang25f01062008-11-10 04:46:22 +00001852bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001853 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001854 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001855 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001856 return false;
1857
Chris Lattner1dcb6542012-01-25 23:49:49 +00001858 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001859 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001860 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001861 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001862
1863 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001864 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1865 return true;
1866
Sanjay Patel8bd52282017-04-19 16:22:19 +00001867 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001868 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001869 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001870 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001871 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001872 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001873 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001874 return false;
1875 }
1876 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001877 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001878 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001879
Sanjay Patel8bd52282017-04-19 16:22:19 +00001880 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001881 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1882 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1883 if (CDS->getElementAsInteger(i) >= V1Size*2)
1884 return false;
1885 return true;
1886 }
1887
1888 // The bitcode reader can create a place holder for a forward reference
1889 // used as the shuffle mask. When this occurs, the shuffle mask will
1890 // fall into this case and fail. To avoid this error, do this bit of
1891 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001892 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001893 if (CE->getOpcode() == Instruction::UserOp1)
1894 return true;
1895
1896 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001897}
1898
Chris Lattnercf129702012-01-26 02:51:13 +00001899int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1900 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001901 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001902 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001903 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001904 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001905 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001906 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001907}
1908
Chris Lattnercf129702012-01-26 02:51:13 +00001909void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1910 SmallVectorImpl<int> &Result) {
1911 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001912
Sanjay Patel8bd52282017-04-19 16:22:19 +00001913 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001914 for (unsigned i = 0; i != NumElts; ++i)
1915 Result.push_back(CDS->getElementAsInteger(i));
1916 return;
1917 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001918 for (unsigned i = 0; i != NumElts; ++i) {
1919 Constant *C = Mask->getAggregateElement(i);
1920 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001921 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001922 }
1923}
1924
1925
Dan Gohman12fce772008-05-15 19:50:34 +00001926//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001927// InsertValueInst Class
1928//===----------------------------------------------------------------------===//
1929
Jay Foad57aa6362011-07-13 10:26:04 +00001930void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1931 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001932 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001933
1934 // There's no fundamental reason why we require at least one index
1935 // (other than weirdness with &*IdxBegin being invalid; see
1936 // getelementptr's init routine for example). But there's no
1937 // present need to support it.
1938 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1939
1940 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001941 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001942 Op<0>() = Agg;
1943 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001944
Jay Foad57aa6362011-07-13 10:26:04 +00001945 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001946 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001947}
1948
1949InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001950 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001951 OperandTraits<InsertValueInst>::op_begin(this), 2),
1952 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001953 Op<0>() = IVI.getOperand(0);
1954 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001955 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001956}
1957
1958//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001959// ExtractValueInst Class
1960//===----------------------------------------------------------------------===//
1961
Jay Foad57aa6362011-07-13 10:26:04 +00001962void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001963 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001964
Jay Foad57aa6362011-07-13 10:26:04 +00001965 // There's no fundamental reason why we require at least one index.
1966 // But there's no present need to support it.
1967 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001968
Jay Foad57aa6362011-07-13 10:26:04 +00001969 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001970 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001971}
1972
1973ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001974 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001975 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001976 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001977}
1978
Dan Gohman12fce772008-05-15 19:50:34 +00001979// getIndexedType - Returns the type of the element that would be extracted
1980// with an extractvalue instruction with the specified parameters.
1981//
1982// A null type is returned if the indices are invalid for the specified
1983// pointer type.
1984//
Chris Lattner229907c2011-07-18 04:54:35 +00001985Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001986 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001987 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001988 // We can't use CompositeType::indexValid(Index) here.
1989 // indexValid() always returns true for arrays because getelementptr allows
1990 // out-of-bounds indices. Since we don't allow those for extractvalue and
1991 // insertvalue we need to check array indexing manually.
1992 // Since the only other types we can index into are struct types it's just
1993 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001994 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001995 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001996 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001997 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001998 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001999 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002000 } else {
2001 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002002 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002003 }
2004
2005 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002006 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002007 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002008}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002009
2010//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002011// BinaryOperator Class
2012//===----------------------------------------------------------------------===//
2013
Chris Lattner2195fc42007-02-24 00:55:48 +00002014BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002015 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002016 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002017 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002018 OperandTraits<BinaryOperator>::op_begin(this),
2019 OperandTraits<BinaryOperator>::operands(this),
2020 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002021 Op<0>() = S1;
2022 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002023 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002024 setName(Name);
2025}
2026
2027BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002028 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002029 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002030 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002031 OperandTraits<BinaryOperator>::op_begin(this),
2032 OperandTraits<BinaryOperator>::operands(this),
2033 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002034 Op<0>() = S1;
2035 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002036 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002037 setName(Name);
2038}
2039
2040
2041void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002042 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002043 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002044 assert(LHS->getType() == RHS->getType() &&
2045 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002046#ifndef NDEBUG
2047 switch (iType) {
2048 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002049 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002050 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002051 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002052 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002053 "Tried to create an integer operation on a non-integer type!");
2054 break;
2055 case FAdd: case FSub:
2056 case FMul:
2057 assert(getType() == LHS->getType() &&
2058 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002059 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002060 "Tried to create a floating-point operation on a "
2061 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002062 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002063 case UDiv:
2064 case SDiv:
2065 assert(getType() == LHS->getType() &&
2066 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002067 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002068 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002069 "Incorrect operand type (not integer) for S/UDIV");
2070 break;
2071 case FDiv:
2072 assert(getType() == LHS->getType() &&
2073 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002074 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002075 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002076 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002077 case URem:
2078 case SRem:
2079 assert(getType() == LHS->getType() &&
2080 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002081 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002082 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002083 "Incorrect operand type (not integer) for S/UREM");
2084 break;
2085 case FRem:
2086 assert(getType() == LHS->getType() &&
2087 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002088 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002089 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002090 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002091 case Shl:
2092 case LShr:
2093 case AShr:
2094 assert(getType() == LHS->getType() &&
2095 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002096 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002097 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002098 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002099 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002100 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002101 case And: case Or:
2102 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002103 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002104 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002105 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002106 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002107 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002108 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002109 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002110 default:
2111 break;
2112 }
2113#endif
2114}
2115
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002116BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002117 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002118 Instruction *InsertBefore) {
2119 assert(S1->getType() == S2->getType() &&
2120 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002121 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002122}
2123
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002124BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002125 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002126 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002127 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002128 InsertAtEnd->getInstList().push_back(Res);
2129 return Res;
2130}
2131
Dan Gohman5476cfd2009-08-12 16:23:25 +00002132BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002133 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002134 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002135 return new BinaryOperator(Instruction::Sub,
2136 zero, Op,
2137 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002138}
2139
Dan Gohman5476cfd2009-08-12 16:23:25 +00002140BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002141 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002142 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002143 return new BinaryOperator(Instruction::Sub,
2144 zero, Op,
2145 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002146}
2147
Dan Gohman4ab44202009-12-18 02:58:50 +00002148BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2149 Instruction *InsertBefore) {
2150 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2151 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2152}
2153
2154BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2155 BasicBlock *InsertAtEnd) {
2156 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2157 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2158}
2159
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002160BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2161 Instruction *InsertBefore) {
2162 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2163 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2164}
2165
2166BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2167 BasicBlock *InsertAtEnd) {
2168 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2169 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2170}
2171
Dan Gohman5476cfd2009-08-12 16:23:25 +00002172BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002173 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002174 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002175 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002176 Op->getType(), Name, InsertBefore);
2177}
2178
Dan Gohman5476cfd2009-08-12 16:23:25 +00002179BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002180 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002181 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002182 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002183 Op->getType(), Name, InsertAtEnd);
2184}
2185
Dan Gohman5476cfd2009-08-12 16:23:25 +00002186BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002187 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002188 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002189 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002190 Op->getType(), Name, InsertBefore);
2191}
2192
Dan Gohman5476cfd2009-08-12 16:23:25 +00002193BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002194 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002195 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002196 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002197 Op->getType(), Name, InsertAtEnd);
2198}
2199
2200
2201// isConstantAllOnes - Helper function for several functions below
2202static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002203 if (const Constant *C = dyn_cast<Constant>(V))
2204 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002205 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002206}
2207
Owen Andersonbb2501b2009-07-13 22:18:28 +00002208bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002209 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2210 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002211 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002212 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002213 return false;
2214}
2215
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002216bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002217 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2218 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002219 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002220 if (!IgnoreZeroSign)
2221 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2222 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2223 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002224 return false;
2225}
2226
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002227bool BinaryOperator::isNot(const Value *V) {
2228 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2229 return (Bop->getOpcode() == Instruction::Xor &&
2230 (isConstantAllOnes(Bop->getOperand(1)) ||
2231 isConstantAllOnes(Bop->getOperand(0))));
2232 return false;
2233}
2234
Chris Lattner2c7d1772005-04-24 07:28:37 +00002235Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002236 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002237}
2238
Chris Lattner2c7d1772005-04-24 07:28:37 +00002239const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2240 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002241}
2242
Dan Gohmana5b96452009-06-04 22:49:04 +00002243Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002244 return cast<BinaryOperator>(BinOp)->getOperand(1);
2245}
2246
2247const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2248 return getFNegArgument(const_cast<Value*>(BinOp));
2249}
2250
Chris Lattner2c7d1772005-04-24 07:28:37 +00002251Value *BinaryOperator::getNotArgument(Value *BinOp) {
2252 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2253 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2254 Value *Op0 = BO->getOperand(0);
2255 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002256 if (isConstantAllOnes(Op0)) return Op1;
2257
2258 assert(isConstantAllOnes(Op1));
2259 return Op0;
2260}
2261
Chris Lattner2c7d1772005-04-24 07:28:37 +00002262const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2263 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002264}
2265
2266
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002267// Exchange the two operands to this instruction. This instruction is safe to
2268// use on any binary instruction and does not modify the semantics of the
2269// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2270// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002271bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002272 if (!isCommutative())
2273 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002274 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002275 return false;
2276}
2277
Sanjay Patela982d992014-09-03 01:06:50 +00002278
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002279//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002280// FPMathOperator Class
2281//===----------------------------------------------------------------------===//
2282
Duncan Sands05f4df82012-04-16 16:28:59 +00002283float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002284 const MDNode *MD =
2285 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002286 if (!MD)
2287 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002288 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002289 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002290}
2291
2292
2293//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002294// CastInst Class
2295//===----------------------------------------------------------------------===//
2296
David Blaikie3a15e142011-12-01 08:00:17 +00002297void CastInst::anchor() {}
2298
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299// Just determine if this cast only deals with integral->integral conversion.
2300bool CastInst::isIntegerCast() const {
2301 switch (getOpcode()) {
2302 default: return false;
2303 case Instruction::ZExt:
2304 case Instruction::SExt:
2305 case Instruction::Trunc:
2306 return true;
2307 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002308 return getOperand(0)->getType()->isIntegerTy() &&
2309 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002311}
2312
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313bool CastInst::isLosslessCast() const {
2314 // Only BitCast can be lossless, exit fast if we're not BitCast
2315 if (getOpcode() != Instruction::BitCast)
2316 return false;
2317
2318 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002319 Type *SrcTy = getOperand(0)->getType();
2320 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002321 if (SrcTy == DstTy)
2322 return true;
2323
Reid Spencer8d9336d2006-12-31 05:26:44 +00002324 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002325 if (SrcTy->isPointerTy())
2326 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002327 return false; // Other types have no identity values
2328}
2329
2330/// This function determines if the CastInst does not require any bits to be
2331/// changed in order to effect the cast. Essentially, it identifies cases where
2332/// no code gen is necessary for the cast, hence the name no-op cast. For
2333/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002334/// # bitcast i32* %x to i8*
2335/// # bitcast <2 x i32> %x to <4 x i16>
2336/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002337/// @brief Determine if the described cast is a no-op.
2338bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002339 Type *SrcTy,
2340 Type *DestTy,
2341 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002342 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002343 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 case Instruction::Trunc:
2345 case Instruction::ZExt:
2346 case Instruction::SExt:
2347 case Instruction::FPTrunc:
2348 case Instruction::FPExt:
2349 case Instruction::UIToFP:
2350 case Instruction::SIToFP:
2351 case Instruction::FPToUI:
2352 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002353 case Instruction::AddrSpaceCast:
2354 // TODO: Target informations may give a more accurate answer here.
2355 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356 case Instruction::BitCast:
2357 return true; // BitCast never modifies bits.
2358 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002359 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002360 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002362 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002363 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364 }
2365}
2366
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002367/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002368bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002369 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2370}
2371
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002372bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002373 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002374 if (getOpcode() == Instruction::PtrToInt)
2375 PtrOpTy = getOperand(0)->getType();
2376 else if (getOpcode() == Instruction::IntToPtr)
2377 PtrOpTy = getType();
2378
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002379 Type *IntPtrTy =
2380 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002381
2382 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2383}
2384
2385/// This function determines if a pair of casts can be eliminated and what
2386/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387/// instructions like this:
2388/// * %F = firstOpcode SrcTy %x to MidTy
2389/// * %S = secondOpcode MidTy %F to DstTy
2390/// The function returns a resultOpcode so these two casts can be replaced with:
2391/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002392/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393unsigned CastInst::isEliminableCastPair(
2394 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002395 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2396 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397 // Define the 144 possibilities for these two cast instructions. The values
2398 // in this matrix determine what to do in a given situation and select the
2399 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002400 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 // the following cast properties:
2402 //
2403 // Size Compare Source Destination
2404 // Operator Src ? Size Type Sign Type Sign
2405 // -------- ------------ ------------------- ---------------------
2406 // TRUNC > Integer Any Integral Any
2407 // ZEXT < Integral Unsigned Integer Any
2408 // SEXT < Integral Signed Integer Any
2409 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002410 // FPTOSI n/a FloatPt n/a Integral Signed
2411 // UITOFP n/a Integral Unsigned FloatPt n/a
2412 // SITOFP n/a Integral Signed FloatPt n/a
2413 // FPTRUNC > FloatPt n/a FloatPt n/a
2414 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415 // PTRTOINT n/a Pointer n/a Integral Unsigned
2416 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002417 // BITCAST = FirstClass n/a FirstClass n/a
2418 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002419 //
2420 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002421 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2422 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002423 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002424 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002425 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002426 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002427 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2429 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002430 // T F F U S F F P I B A -+
2431 // R Z S P P I I T P 2 N T S |
2432 // U E E 2 2 2 2 R E I T C C +- secondOp
2433 // N X X U S F F N X N 2 V V |
2434 // C T T I I P P C T T P T T -+
2435 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002436 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002437 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2438 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2439 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2440 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2441 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002442 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002443 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2444 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2445 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2446 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2447 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002448 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002449
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002450 // TODO: This logic could be encoded into the table above and handled in the
2451 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002452 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002453 // merging. However, any pair of bitcasts are allowed.
2454 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2455 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2456 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002457
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002458 // Check if any of the casts convert scalars <-> vectors.
2459 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2460 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2461 if (!AreBothBitcasts)
2462 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463
2464 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2465 [secondOp-Instruction::CastOpsBegin];
2466 switch (ElimCase) {
2467 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002468 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 return 0;
2470 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002471 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 return firstOp;
2473 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002474 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475 return secondOp;
2476 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002477 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002478 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002479 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002480 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481 return firstOp;
2482 return 0;
2483 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002484 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002485 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002486 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487 return firstOp;
2488 return 0;
2489 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002490 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002491 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002492 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493 return secondOp;
2494 return 0;
2495 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002496 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002497 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002498 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499 return secondOp;
2500 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002501 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002502 // Cannot simplify if address spaces are different!
2503 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2504 return 0;
2505
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002506 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002507 // We can still fold this without knowing the actual sizes as long we
2508 // know that the intermediate pointer is the largest possible
2509 // pointer size.
2510 // FIXME: Is this always true?
2511 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002512 return Instruction::BitCast;
2513
2514 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002515 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002516 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002517 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518 if (MidSize >= PtrSize)
2519 return Instruction::BitCast;
2520 return 0;
2521 }
2522 case 8: {
2523 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2524 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2525 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002526 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2527 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528 if (SrcSize == DstSize)
2529 return Instruction::BitCast;
2530 else if (SrcSize < DstSize)
2531 return firstOp;
2532 return secondOp;
2533 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002534 case 9:
2535 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536 return Instruction::ZExt;
2537 case 10:
2538 // fpext followed by ftrunc is allowed if the bit size returned to is
2539 // the same as the original, in which case its just a bitcast
2540 if (SrcTy == DstTy)
2541 return Instruction::BitCast;
2542 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002543 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002545 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002546 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002547 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002548 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2549 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550 if (SrcSize <= PtrSize && SrcSize == DstSize)
2551 return Instruction::BitCast;
2552 return 0;
2553 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002554 case 12: {
2555 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2556 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2557 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2558 return Instruction::AddrSpaceCast;
2559 return Instruction::BitCast;
2560 }
2561 case 13:
2562 // FIXME: this state can be merged with (1), but the following assert
2563 // is useful to check the correcteness of the sequence due to semantic
2564 // change of bitcast.
2565 assert(
2566 SrcTy->isPtrOrPtrVectorTy() &&
2567 MidTy->isPtrOrPtrVectorTy() &&
2568 DstTy->isPtrOrPtrVectorTy() &&
2569 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2570 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2571 "Illegal addrspacecast, bitcast sequence!");
2572 // Allowed, use first cast's opcode
2573 return firstOp;
2574 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002575 // bitcast, addrspacecast -> addrspacecast if the element type of
2576 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002577 if (SrcTy->getScalarType()->getPointerElementType() ==
2578 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002579 return Instruction::AddrSpaceCast;
2580 return 0;
2581
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002582 case 15:
2583 // FIXME: this state can be merged with (1), but the following assert
2584 // is useful to check the correcteness of the sequence due to semantic
2585 // change of bitcast.
2586 assert(
2587 SrcTy->isIntOrIntVectorTy() &&
2588 MidTy->isPtrOrPtrVectorTy() &&
2589 DstTy->isPtrOrPtrVectorTy() &&
2590 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2591 "Illegal inttoptr, bitcast sequence!");
2592 // Allowed, use first cast's opcode
2593 return firstOp;
2594 case 16:
2595 // FIXME: this state can be merged with (2), but the following assert
2596 // is useful to check the correcteness of the sequence due to semantic
2597 // change of bitcast.
2598 assert(
2599 SrcTy->isPtrOrPtrVectorTy() &&
2600 MidTy->isPtrOrPtrVectorTy() &&
2601 DstTy->isIntOrIntVectorTy() &&
2602 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2603 "Illegal bitcast, ptrtoint sequence!");
2604 // Allowed, use second cast's opcode
2605 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002606 case 17:
2607 // (sitofp (zext x)) -> (uitofp x)
2608 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002610 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002612 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613 default:
Craig Topperc514b542012-02-05 22:14:15 +00002614 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616}
2617
Chris Lattner229907c2011-07-18 04:54:35 +00002618CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002619 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002620 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621 // Construct and return the appropriate CastInst subclass
2622 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002623 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2624 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2625 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2626 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2627 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2628 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2629 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2630 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2631 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2632 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2633 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2634 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2635 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2636 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002638}
2639
Chris Lattner229907c2011-07-18 04:54:35 +00002640CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002641 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002642 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643 // Construct and return the appropriate CastInst subclass
2644 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002645 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2646 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2647 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2648 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2649 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2650 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2651 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2652 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2653 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2654 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2655 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2656 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2657 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2658 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660}
2661
Chris Lattner229907c2011-07-18 04:54:35 +00002662CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002663 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002664 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002665 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002666 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2667 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002668}
2669
Chris Lattner229907c2011-07-18 04:54:35 +00002670CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002671 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002672 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002673 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002674 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2675 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002676}
2677
Chris Lattner229907c2011-07-18 04:54:35 +00002678CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002679 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002680 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002681 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002682 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2683 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002684}
2685
Chris Lattner229907c2011-07-18 04:54:35 +00002686CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002687 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002688 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002689 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002690 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2691 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002692}
2693
Chris Lattner229907c2011-07-18 04:54:35 +00002694CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002695 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002696 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002697 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002698 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2699 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002700}
2701
Chris Lattner229907c2011-07-18 04:54:35 +00002702CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002703 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002704 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002705 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002706 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2707 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002708}
2709
Chris Lattner229907c2011-07-18 04:54:35 +00002710CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002711 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002712 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002713 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2714 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2715 "Invalid cast");
2716 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002717 assert((!Ty->isVectorTy() ||
2718 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002719 "Invalid cast");
2720
Matt Arsenault065ced92013-07-31 00:17:33 +00002721 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002722 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002723
Matt Arsenault740980e2014-07-14 17:24:35 +00002724 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002725}
2726
2727/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002728CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2729 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002730 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002731 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2732 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002733 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002734 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002735 assert((!Ty->isVectorTy() ||
2736 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002737 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002738
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002739 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002740 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002741
Matt Arsenault740980e2014-07-14 17:24:35 +00002742 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2743}
2744
2745CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2746 Value *S, Type *Ty,
2747 const Twine &Name,
2748 BasicBlock *InsertAtEnd) {
2749 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2750 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2751
2752 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2753 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2754
2755 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2756}
2757
2758CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2759 Value *S, Type *Ty,
2760 const Twine &Name,
2761 Instruction *InsertBefore) {
2762 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2763 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2764
2765 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002766 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2767
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002768 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002769}
2770
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002771CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2772 const Twine &Name,
2773 Instruction *InsertBefore) {
2774 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2775 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2776 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2777 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2778
2779 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2780}
2781
Matt Arsenault740980e2014-07-14 17:24:35 +00002782CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002783 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002784 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002785 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002786 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002787 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2788 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002789 Instruction::CastOps opcode =
2790 (SrcBits == DstBits ? Instruction::BitCast :
2791 (SrcBits > DstBits ? Instruction::Trunc :
2792 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002793 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002794}
2795
Chris Lattner229907c2011-07-18 04:54:35 +00002796CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002797 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002798 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002799 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002800 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002801 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2802 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002803 Instruction::CastOps opcode =
2804 (SrcBits == DstBits ? Instruction::BitCast :
2805 (SrcBits > DstBits ? Instruction::Trunc :
2806 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002807 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002808}
2809
Chris Lattner229907c2011-07-18 04:54:35 +00002810CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002811 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002812 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002813 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002814 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002815 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2816 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002817 Instruction::CastOps opcode =
2818 (SrcBits == DstBits ? Instruction::BitCast :
2819 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002820 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002821}
2822
Chris Lattner229907c2011-07-18 04:54:35 +00002823CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002824 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002825 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002826 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002827 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002828 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2829 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002830 Instruction::CastOps opcode =
2831 (SrcBits == DstBits ? Instruction::BitCast :
2832 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002833 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002834}
2835
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002836// Check whether it is valid to call getCastOpcode for these types.
2837// This routine must be kept in sync with getCastOpcode.
2838bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2839 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2840 return false;
2841
2842 if (SrcTy == DestTy)
2843 return true;
2844
2845 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2846 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2847 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2848 // An element by element cast. Valid if casting the elements is valid.
2849 SrcTy = SrcVecTy->getElementType();
2850 DestTy = DestVecTy->getElementType();
2851 }
2852
2853 // Get the bit sizes, we'll need these
2854 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2855 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2856
2857 // Run through the possibilities ...
2858 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002859 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002860 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002861 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002862 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002863 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002864 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002865 // Casting from something else
2866 return SrcTy->isPointerTy();
2867 }
2868 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2869 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002870 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002871 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002872 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002873 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002874 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002875 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002876 return false;
2877 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002878 if (DestTy->isVectorTy()) // Casting to vector
2879 return DestBits == SrcBits;
2880 if (DestTy->isPointerTy()) { // Casting to pointer
2881 if (SrcTy->isPointerTy()) // Casting from pointer
2882 return true;
2883 return SrcTy->isIntegerTy(); // Casting from integral
2884 }
2885 if (DestTy->isX86_MMXTy()) {
2886 if (SrcTy->isVectorTy())
2887 return DestBits == SrcBits; // 64-bit vector to MMX
2888 return false;
2889 } // Casting to something else
2890 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002891}
2892
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002893bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2894 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2895 return false;
2896
2897 if (SrcTy == DestTy)
2898 return true;
2899
2900 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2901 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2902 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2903 // An element by element cast. Valid if casting the elements is valid.
2904 SrcTy = SrcVecTy->getElementType();
2905 DestTy = DestVecTy->getElementType();
2906 }
2907 }
2908 }
2909
2910 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2911 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2912 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2913 }
2914 }
2915
2916 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2917 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2918
2919 // Could still have vectors of pointers if the number of elements doesn't
2920 // match
2921 if (SrcBits == 0 || DestBits == 0)
2922 return false;
2923
2924 if (SrcBits != DestBits)
2925 return false;
2926
2927 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2928 return false;
2929
2930 return true;
2931}
2932
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002933bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002934 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002935 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2936 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002937 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002938 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2939 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002940 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002941
2942 return isBitCastable(SrcTy, DestTy);
2943}
2944
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002945// Provide a way to get a "cast" where the cast opcode is inferred from the
2946// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002947// logic in the castIsValid function below. This axiom should hold:
2948// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2949// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002950// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002951// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002952Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002953CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002954 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2955 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002956
Duncan Sands55e50902008-01-06 10:12:28 +00002957 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2958 "Only first class types are castable!");
2959
Duncan Sandsa8514532011-05-18 07:13:41 +00002960 if (SrcTy == DestTy)
2961 return BitCast;
2962
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002963 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002964 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2965 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002966 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2967 // An element by element cast. Find the appropriate opcode based on the
2968 // element types.
2969 SrcTy = SrcVecTy->getElementType();
2970 DestTy = DestVecTy->getElementType();
2971 }
2972
2973 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002974 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2975 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002976
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002977 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002978 if (DestTy->isIntegerTy()) { // Casting to integral
2979 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002980 if (DestBits < SrcBits)
2981 return Trunc; // int -> smaller int
2982 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002983 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002984 return SExt; // signed -> SEXT
2985 else
2986 return ZExt; // unsigned -> ZEXT
2987 } else {
2988 return BitCast; // Same size, No-op cast
2989 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002990 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002991 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002992 return FPToSI; // FP -> sint
2993 else
2994 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002995 } else if (SrcTy->isVectorTy()) {
2996 assert(DestBits == SrcBits &&
2997 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002998 return BitCast; // Same size, no-op cast
2999 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003000 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003001 "Casting from a value that is not first-class type");
3002 return PtrToInt; // ptr -> int
3003 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003004 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3005 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003006 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003007 return SIToFP; // sint -> FP
3008 else
3009 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003010 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011 if (DestBits < SrcBits) {
3012 return FPTrunc; // FP -> smaller FP
3013 } else if (DestBits > SrcBits) {
3014 return FPExt; // FP -> larger FP
3015 } else {
3016 return BitCast; // same size, no-op cast
3017 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003018 } else if (SrcTy->isVectorTy()) {
3019 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003020 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003021 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003022 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003023 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003024 } else if (DestTy->isVectorTy()) {
3025 assert(DestBits == SrcBits &&
3026 "Illegal cast to vector (wrong type or size)");
3027 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003028 } else if (DestTy->isPointerTy()) {
3029 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003030 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3031 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003032 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003033 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003034 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003035 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003036 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003037 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003038 if (SrcTy->isVectorTy()) {
3039 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003040 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003041 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003042 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003043 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003044 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003045}
3046
3047//===----------------------------------------------------------------------===//
3048// CastInst SubClass Constructors
3049//===----------------------------------------------------------------------===//
3050
3051/// Check that the construction parameters for a CastInst are correct. This
3052/// could be broken out into the separate constructors but it is useful to have
3053/// it in one place and to eliminate the redundant code for getting the sizes
3054/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003055bool
Chris Lattner229907c2011-07-18 04:54:35 +00003056CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003057
3058 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003059 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003060
Chris Lattner37bc78a2010-01-26 21:51:43 +00003061 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3062 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003063 return false;
3064
3065 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003066 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3067 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003068
Duncan Sands7f646562011-05-18 09:21:57 +00003069 // If these are vector types, get the lengths of the vectors (using zero for
3070 // scalar types means that checking that vector lengths match also checks that
3071 // scalars are not being converted to vectors or vectors to scalars).
3072 unsigned SrcLength = SrcTy->isVectorTy() ?
3073 cast<VectorType>(SrcTy)->getNumElements() : 0;
3074 unsigned DstLength = DstTy->isVectorTy() ?
3075 cast<VectorType>(DstTy)->getNumElements() : 0;
3076
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003077 // Switch on the opcode provided
3078 switch (op) {
3079 default: return false; // This is an input error
3080 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003081 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3082 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003083 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003084 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3085 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003086 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003087 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3088 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003089 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003090 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3091 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003092 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003093 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3094 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003095 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003096 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003097 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3098 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003099 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003100 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003101 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3102 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003103 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003104 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003105 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003106 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3107 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3108 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003109 return SrcTy->getScalarType()->isPointerTy() &&
3110 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003111 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003112 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003113 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003114 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3115 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3116 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003117 return SrcTy->getScalarType()->isIntegerTy() &&
3118 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003119 case Instruction::BitCast: {
3120 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3121 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3122
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003123 // BitCast implies a no-op cast of type only. No bits change.
3124 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003125 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003126 return false;
3127
Alp Tokerf907b892013-12-05 05:44:44 +00003128 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003129 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003130 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003131 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3132
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003133 // If both are pointers then the address spaces must match.
3134 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3135 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003136
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003137 // A vector of pointers must have the same number of elements.
3138 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3139 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3140 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3141
3142 return false;
3143 }
3144
3145 return true;
3146 }
3147 case Instruction::AddrSpaceCast: {
3148 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3149 if (!SrcPtrTy)
3150 return false;
3151
3152 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3153 if (!DstPtrTy)
3154 return false;
3155
3156 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3157 return false;
3158
3159 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3160 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3161 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3162
3163 return false;
3164 }
3165
3166 return true;
3167 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168 }
3169}
3170
3171TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003172 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003173) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003174 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003175}
3176
3177TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003178 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003179) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003180 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003181}
3182
3183ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003184 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003185) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003186 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003187}
3188
3189ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003190 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003191) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003192 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003193}
3194SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003195 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003196) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003197 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003198}
3199
Jeff Cohencc08c832006-12-02 02:22:01 +00003200SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003201 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003202) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003203 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003204}
3205
3206FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003207 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003208) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003209 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003210}
3211
3212FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003213 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003214) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003215 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003216}
3217
3218FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003219 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003220) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003221 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003222}
3223
3224FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003225 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003226) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003227 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228}
3229
3230UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003231 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003232) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003233 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234}
3235
3236UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003237 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003238) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003239 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240}
3241
3242SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003243 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003244) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003245 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003246}
3247
3248SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003249 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003250) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003251 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003252}
3253
3254FPToUIInst::FPToUIInst(
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, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003257 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258}
3259
3260FPToUIInst::FPToUIInst(
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, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003263 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264}
3265
3266FPToSIInst::FPToSIInst(
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, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003269 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270}
3271
3272FPToSIInst::FPToSIInst(
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, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003275 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276}
3277
3278PtrToIntInst::PtrToIntInst(
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, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003281 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282}
3283
3284PtrToIntInst::PtrToIntInst(
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, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003287 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003288}
3289
3290IntToPtrInst::IntToPtrInst(
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, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003293 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003294}
3295
3296IntToPtrInst::IntToPtrInst(
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, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003299 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003300}
3301
3302BitCastInst::BitCastInst(
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, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003305 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003306}
3307
3308BitCastInst::BitCastInst(
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, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003311 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003312}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003313
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003314AddrSpaceCastInst::AddrSpaceCastInst(
3315 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3316) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3317 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3318}
3319
3320AddrSpaceCastInst::AddrSpaceCastInst(
3321 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3322) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3323 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3324}
3325
Chris Lattnerf16dc002006-09-17 19:29:56 +00003326//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003327// CmpInst Classes
3328//===----------------------------------------------------------------------===//
3329
Craig Topper3186c012012-09-23 02:12:10 +00003330void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003331
Craig Topper1c3f2832015-12-15 06:11:33 +00003332CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3333 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003334 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003335 OperandTraits<CmpInst>::op_begin(this),
3336 OperandTraits<CmpInst>::operands(this),
3337 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003338 Op<0>() = LHS;
3339 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003340 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003341 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003342}
Gabor Greiff6caff662008-05-10 08:32:32 +00003343
Craig Topper1c3f2832015-12-15 06:11:33 +00003344CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3345 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003346 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003347 OperandTraits<CmpInst>::op_begin(this),
3348 OperandTraits<CmpInst>::operands(this),
3349 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003350 Op<0>() = LHS;
3351 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003352 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003353 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003354}
3355
3356CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003357CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003358 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003359 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003360 if (InsertBefore)
3361 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3362 S1, S2, Name);
3363 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003364 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003365 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003366 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003367
3368 if (InsertBefore)
3369 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3370 S1, S2, Name);
3371 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003372 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003373 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003374}
3375
3376CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003377CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003378 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003379 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003380 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3381 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003382 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003383 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3384 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003385}
3386
3387void CmpInst::swapOperands() {
3388 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3389 IC->swapOperands();
3390 else
3391 cast<FCmpInst>(this)->swapOperands();
3392}
3393
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003394bool CmpInst::isCommutative() const {
3395 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003396 return IC->isCommutative();
3397 return cast<FCmpInst>(this)->isCommutative();
3398}
3399
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003400bool CmpInst::isEquality() const {
3401 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003402 return IC->isEquality();
3403 return cast<FCmpInst>(this)->isEquality();
3404}
3405
3406
Dan Gohman4e724382008-05-31 02:47:54 +00003407CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003408 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003409 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003410 case ICMP_EQ: return ICMP_NE;
3411 case ICMP_NE: return ICMP_EQ;
3412 case ICMP_UGT: return ICMP_ULE;
3413 case ICMP_ULT: return ICMP_UGE;
3414 case ICMP_UGE: return ICMP_ULT;
3415 case ICMP_ULE: return ICMP_UGT;
3416 case ICMP_SGT: return ICMP_SLE;
3417 case ICMP_SLT: return ICMP_SGE;
3418 case ICMP_SGE: return ICMP_SLT;
3419 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003420
Dan Gohman4e724382008-05-31 02:47:54 +00003421 case FCMP_OEQ: return FCMP_UNE;
3422 case FCMP_ONE: return FCMP_UEQ;
3423 case FCMP_OGT: return FCMP_ULE;
3424 case FCMP_OLT: return FCMP_UGE;
3425 case FCMP_OGE: return FCMP_ULT;
3426 case FCMP_OLE: return FCMP_UGT;
3427 case FCMP_UEQ: return FCMP_ONE;
3428 case FCMP_UNE: return FCMP_OEQ;
3429 case FCMP_UGT: return FCMP_OLE;
3430 case FCMP_ULT: return FCMP_OGE;
3431 case FCMP_UGE: return FCMP_OLT;
3432 case FCMP_ULE: return FCMP_OGT;
3433 case FCMP_ORD: return FCMP_UNO;
3434 case FCMP_UNO: return FCMP_ORD;
3435 case FCMP_TRUE: return FCMP_FALSE;
3436 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003437 }
3438}
3439
Tim Northoverde3aea0412016-08-17 20:25:25 +00003440StringRef CmpInst::getPredicateName(Predicate Pred) {
3441 switch (Pred) {
3442 default: return "unknown";
3443 case FCmpInst::FCMP_FALSE: return "false";
3444 case FCmpInst::FCMP_OEQ: return "oeq";
3445 case FCmpInst::FCMP_OGT: return "ogt";
3446 case FCmpInst::FCMP_OGE: return "oge";
3447 case FCmpInst::FCMP_OLT: return "olt";
3448 case FCmpInst::FCMP_OLE: return "ole";
3449 case FCmpInst::FCMP_ONE: return "one";
3450 case FCmpInst::FCMP_ORD: return "ord";
3451 case FCmpInst::FCMP_UNO: return "uno";
3452 case FCmpInst::FCMP_UEQ: return "ueq";
3453 case FCmpInst::FCMP_UGT: return "ugt";
3454 case FCmpInst::FCMP_UGE: return "uge";
3455 case FCmpInst::FCMP_ULT: return "ult";
3456 case FCmpInst::FCMP_ULE: return "ule";
3457 case FCmpInst::FCMP_UNE: return "une";
3458 case FCmpInst::FCMP_TRUE: return "true";
3459 case ICmpInst::ICMP_EQ: return "eq";
3460 case ICmpInst::ICMP_NE: return "ne";
3461 case ICmpInst::ICMP_SGT: return "sgt";
3462 case ICmpInst::ICMP_SGE: return "sge";
3463 case ICmpInst::ICMP_SLT: return "slt";
3464 case ICmpInst::ICMP_SLE: return "sle";
3465 case ICmpInst::ICMP_UGT: return "ugt";
3466 case ICmpInst::ICMP_UGE: return "uge";
3467 case ICmpInst::ICMP_ULT: return "ult";
3468 case ICmpInst::ICMP_ULE: return "ule";
3469 }
3470}
3471
Pete Cooper1d078692015-12-14 20:29:16 +00003472void ICmpInst::anchor() {}
3473
Reid Spencer266e42b2006-12-23 06:05:41 +00003474ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3475 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003476 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003477 case ICMP_EQ: case ICMP_NE:
3478 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3479 return pred;
3480 case ICMP_UGT: return ICMP_SGT;
3481 case ICMP_ULT: return ICMP_SLT;
3482 case ICMP_UGE: return ICMP_SGE;
3483 case ICMP_ULE: return ICMP_SLE;
3484 }
3485}
3486
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003487ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3488 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003489 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003490 case ICMP_EQ: case ICMP_NE:
3491 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3492 return pred;
3493 case ICMP_SGT: return ICMP_UGT;
3494 case ICMP_SLT: return ICMP_ULT;
3495 case ICMP_SGE: return ICMP_UGE;
3496 case ICMP_SLE: return ICMP_ULE;
3497 }
3498}
3499
Dan Gohman4e724382008-05-31 02:47:54 +00003500CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003501 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003502 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003503 case ICMP_EQ: case ICMP_NE:
3504 return pred;
3505 case ICMP_SGT: return ICMP_SLT;
3506 case ICMP_SLT: return ICMP_SGT;
3507 case ICMP_SGE: return ICMP_SLE;
3508 case ICMP_SLE: return ICMP_SGE;
3509 case ICMP_UGT: return ICMP_ULT;
3510 case ICMP_ULT: return ICMP_UGT;
3511 case ICMP_UGE: return ICMP_ULE;
3512 case ICMP_ULE: return ICMP_UGE;
3513
Reid Spencerd9436b62006-11-20 01:22:35 +00003514 case FCMP_FALSE: case FCMP_TRUE:
3515 case FCMP_OEQ: case FCMP_ONE:
3516 case FCMP_UEQ: case FCMP_UNE:
3517 case FCMP_ORD: case FCMP_UNO:
3518 return pred;
3519 case FCMP_OGT: return FCMP_OLT;
3520 case FCMP_OLT: return FCMP_OGT;
3521 case FCMP_OGE: return FCMP_OLE;
3522 case FCMP_OLE: return FCMP_OGE;
3523 case FCMP_UGT: return FCMP_ULT;
3524 case FCMP_ULT: return FCMP_UGT;
3525 case FCMP_UGE: return FCMP_ULE;
3526 case FCMP_ULE: return FCMP_UGE;
3527 }
3528}
3529
Sanjoy Das6e78b172015-10-22 19:57:34 +00003530CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3531 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3532
3533 switch (pred) {
3534 default:
3535 llvm_unreachable("Unknown predicate!");
3536 case CmpInst::ICMP_ULT:
3537 return CmpInst::ICMP_SLT;
3538 case CmpInst::ICMP_ULE:
3539 return CmpInst::ICMP_SLE;
3540 case CmpInst::ICMP_UGT:
3541 return CmpInst::ICMP_SGT;
3542 case CmpInst::ICMP_UGE:
3543 return CmpInst::ICMP_SGE;
3544 }
3545}
3546
Craig Topper1c3f2832015-12-15 06:11:33 +00003547bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003548 switch (predicate) {
3549 default: return false;
3550 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3551 case ICmpInst::ICMP_UGE: return true;
3552 }
3553}
3554
Craig Topper1c3f2832015-12-15 06:11:33 +00003555bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003556 switch (predicate) {
3557 default: return false;
3558 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3559 case ICmpInst::ICMP_SGE: return true;
3560 }
3561}
3562
Craig Topper1c3f2832015-12-15 06:11:33 +00003563bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003564 switch (predicate) {
3565 default: return false;
3566 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3567 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3568 case FCmpInst::FCMP_ORD: return true;
3569 }
3570}
3571
Craig Topper1c3f2832015-12-15 06:11:33 +00003572bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003573 switch (predicate) {
3574 default: return false;
3575 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3576 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3577 case FCmpInst::FCMP_UNO: return true;
3578 }
3579}
3580
Craig Topper1c3f2832015-12-15 06:11:33 +00003581bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003582 switch(predicate) {
3583 default: return false;
3584 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3585 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3586 }
3587}
3588
Craig Topper1c3f2832015-12-15 06:11:33 +00003589bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003590 switch(predicate) {
3591 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3592 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3593 default: return false;
3594 }
3595}
3596
Chad Rosier99bc4802016-04-21 16:18:02 +00003597bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003598 // If the predicates match, then we know the first condition implies the
3599 // second is true.
3600 if (Pred1 == Pred2)
3601 return true;
3602
3603 switch (Pred1) {
3604 default:
3605 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003606 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003607 // 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 +00003608 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3609 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003610 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3611 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3612 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3613 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3614 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3615 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3616 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3617 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003618 }
3619 return false;
3620}
3621
Chad Rosier99bc4802016-04-21 16:18:02 +00003622bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003623 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003624}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003625
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003626//===----------------------------------------------------------------------===//
3627// SwitchInst Implementation
3628//===----------------------------------------------------------------------===//
3629
Chris Lattnerbaf00152010-11-17 05:41:46 +00003630void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3631 assert(Value && Default && NumReserved);
3632 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003633 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003634 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003635
Pete Coopereb31b682015-05-21 22:48:54 +00003636 Op<0>() = Value;
3637 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003638}
3639
Chris Lattner2195fc42007-02-24 00:55:48 +00003640/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3641/// switch on and a default destination. The number of additional cases can
3642/// be specified here to make memory allocation more efficient. This
3643/// constructor can also autoinsert before another instruction.
3644SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3645 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003646 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003647 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003648 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003649}
3650
3651/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3652/// switch on and a default destination. The number of additional cases can
3653/// be specified here to make memory allocation more efficient. This
3654/// constructor also autoinserts at the end of the specified BasicBlock.
3655SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3656 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003657 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003658 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003659 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003660}
3661
Misha Brukmanb1c93172005-04-21 23:48:37 +00003662SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003663 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003664 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003665 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003666 Use *OL = getOperandList();
3667 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003668 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003669 OL[i] = InOL[i];
3670 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003671 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003672 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003673}
3674
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003675
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003676/// addCase - Add an entry to the switch instruction...
3677///
Chris Lattner47ac1872005-02-24 05:32:09 +00003678void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003679 unsigned NewCaseIdx = getNumCases();
3680 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003681 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003682 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003683 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003684 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003685 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003686 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003687 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003688 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003689}
3690
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003691/// removeCase - This method removes the specified case and its successor
3692/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003693SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3694 unsigned idx = I->getCaseIndex();
3695
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003696 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003697
3698 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003699 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003700
Jay Foad14277722011-02-01 09:22:34 +00003701 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003702 if (2 + (idx + 1) * 2 != NumOps) {
3703 OL[2 + idx * 2] = OL[NumOps - 2];
3704 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003705 }
3706
3707 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003708 OL[NumOps-2].set(nullptr);
3709 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003710 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003711
3712 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003713}
3714
Jay Foade98f29d2011-04-01 08:00:58 +00003715/// growOperands - grow operands - This grows the operand list in response
3716/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003717///
Jay Foade98f29d2011-04-01 08:00:58 +00003718void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003719 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003720 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003721
3722 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003723 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003724}
3725
3726
3727BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3728 return getSuccessor(idx);
3729}
3730unsigned SwitchInst::getNumSuccessorsV() const {
3731 return getNumSuccessors();
3732}
3733void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3734 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003735}
Chris Lattnerf22be932004-10-15 23:52:53 +00003736
Chris Lattner3ed871f2009-10-27 19:13:16 +00003737//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003738// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003739//===----------------------------------------------------------------------===//
3740
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003741void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003742 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003743 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003744 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003745 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003746 allocHungoffUses(ReservedSpace);
3747
Pete Coopereb31b682015-05-21 22:48:54 +00003748 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003749}
3750
3751
Jay Foade98f29d2011-04-01 08:00:58 +00003752/// growOperands - grow operands - This grows the operand list in response
3753/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003754///
Jay Foade98f29d2011-04-01 08:00:58 +00003755void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003756 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003757 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003758
3759 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003760 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003761}
3762
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003763IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3764 Instruction *InsertBefore)
3765: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003766 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003767 init(Address, NumCases);
3768}
3769
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003770IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3771 BasicBlock *InsertAtEnd)
3772: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003773 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003774 init(Address, NumCases);
3775}
3776
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003777IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003778 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3779 nullptr, IBI.getNumOperands()) {
3780 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003781 Use *OL = getOperandList();
3782 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003783 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3784 OL[i] = InOL[i];
3785 SubclassOptionalData = IBI.SubclassOptionalData;
3786}
3787
Chris Lattner3ed871f2009-10-27 19:13:16 +00003788/// addDestination - Add a destination.
3789///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003790void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003791 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003792 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003793 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003794 // Initialize some new operands.
3795 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003796 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003797 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003798}
3799
3800/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003801/// indirectbr instruction.
3802void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003803 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3804
3805 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003806 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003807
3808 // Replace this value with the last one.
3809 OL[idx+1] = OL[NumOps-1];
3810
3811 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003812 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003813 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003814}
3815
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003816BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003817 return getSuccessor(idx);
3818}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003819unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003820 return getNumSuccessors();
3821}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003822void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003823 setSuccessor(idx, B);
3824}
3825
3826//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003827// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003828//===----------------------------------------------------------------------===//
3829
Chris Lattnerf22be932004-10-15 23:52:53 +00003830// Define these methods here so vtables don't get emitted into every translation
3831// unit that uses these classes.
3832
Pete Cooper75403d72015-06-24 20:22:23 +00003833GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003834 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003835}
3836
Pete Cooper75403d72015-06-24 20:22:23 +00003837BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003838 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003839}
3840
Pete Cooper75403d72015-06-24 20:22:23 +00003841FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003842 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003843}
3844
Pete Cooper75403d72015-06-24 20:22:23 +00003845ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003846 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003847}
3848
Pete Cooper75403d72015-06-24 20:22:23 +00003849ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003850 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003851}
3852
Pete Cooper75403d72015-06-24 20:22:23 +00003853InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003854 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003855}
3856
Pete Cooper75403d72015-06-24 20:22:23 +00003857AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003858 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003859 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003860 (Value *)getOperand(0), getAlignment());
3861 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003862 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003863 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003864}
3865
Pete Cooper75403d72015-06-24 20:22:23 +00003866LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003867 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3868 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003869}
3870
Pete Cooper75403d72015-06-24 20:22:23 +00003871StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003872 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003873 getAlignment(), getOrdering(), getSynchScope());
3874
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003875}
3876
Pete Cooper75403d72015-06-24 20:22:23 +00003877AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003878 AtomicCmpXchgInst *Result =
3879 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003880 getSuccessOrdering(), getFailureOrdering(),
3881 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003882 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003883 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003884 return Result;
3885}
3886
Pete Cooper75403d72015-06-24 20:22:23 +00003887AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003888 AtomicRMWInst *Result =
3889 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3890 getOrdering(), getSynchScope());
3891 Result->setVolatile(isVolatile());
3892 return Result;
3893}
3894
Pete Cooper75403d72015-06-24 20:22:23 +00003895FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003896 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3897}
3898
Pete Cooper75403d72015-06-24 20:22:23 +00003899TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003900 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003901}
3902
Pete Cooper75403d72015-06-24 20:22:23 +00003903ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003904 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003905}
3906
Pete Cooper75403d72015-06-24 20:22:23 +00003907SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003908 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003909}
3910
Pete Cooper75403d72015-06-24 20:22:23 +00003911FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003912 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003913}
3914
Pete Cooper75403d72015-06-24 20:22:23 +00003915FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003916 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003917}
3918
Pete Cooper75403d72015-06-24 20:22:23 +00003919UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003920 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003921}
3922
Pete Cooper75403d72015-06-24 20:22:23 +00003923SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003924 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003925}
3926
Pete Cooper75403d72015-06-24 20:22:23 +00003927FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003928 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003929}
3930
Pete Cooper75403d72015-06-24 20:22:23 +00003931FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003932 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003933}
3934
Pete Cooper75403d72015-06-24 20:22:23 +00003935PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003936 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003937}
3938
Pete Cooper75403d72015-06-24 20:22:23 +00003939IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003940 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003941}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003942
Pete Cooper75403d72015-06-24 20:22:23 +00003943BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003944 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003945}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003946
Pete Cooper75403d72015-06-24 20:22:23 +00003947AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003948 return new AddrSpaceCastInst(getOperand(0), getType());
3949}
3950
Pete Cooper75403d72015-06-24 20:22:23 +00003951CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003952 if (hasOperandBundles()) {
3953 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3954 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3955 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003956 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003957}
3958
Pete Cooper75403d72015-06-24 20:22:23 +00003959SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003960 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003961}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003962
Pete Cooper75403d72015-06-24 20:22:23 +00003963VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003964 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003965}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003966
Pete Cooper75403d72015-06-24 20:22:23 +00003967ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003968 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003969}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003970
Pete Cooper75403d72015-06-24 20:22:23 +00003971InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003972 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003973}
3974
Pete Cooper75403d72015-06-24 20:22:23 +00003975ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003976 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003977}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003978
Pete Cooper75403d72015-06-24 20:22:23 +00003979PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003980
Pete Cooper75403d72015-06-24 20:22:23 +00003981LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003982 return new LandingPadInst(*this);
3983}
3984
Pete Cooper75403d72015-06-24 20:22:23 +00003985ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003986 return new(getNumOperands()) ReturnInst(*this);
3987}
3988
Pete Cooper75403d72015-06-24 20:22:23 +00003989BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003990 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003991}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003992
Pete Cooper75403d72015-06-24 20:22:23 +00003993SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003994
Pete Cooper75403d72015-06-24 20:22:23 +00003995IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003996 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003997}
3998
Pete Cooper75403d72015-06-24 20:22:23 +00003999InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004000 if (hasOperandBundles()) {
4001 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4002 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4003 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004004 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004005}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004006
Pete Cooper75403d72015-06-24 20:22:23 +00004007ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004008
David Majnemer654e1302015-07-31 17:58:14 +00004009CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4010 return new (getNumOperands()) CleanupReturnInst(*this);
4011}
4012
David Majnemer654e1302015-07-31 17:58:14 +00004013CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004014 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004015}
4016
David Majnemer8a1c45d2015-12-12 05:38:55 +00004017CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
4018 return new CatchSwitchInst(*this);
4019}
4020
4021FuncletPadInst *FuncletPadInst::cloneImpl() const {
4022 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004023}
4024
Pete Cooper75403d72015-06-24 20:22:23 +00004025UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004026 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004027 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004028}